CodeIgniter Forums

Full Version: Form validation: error message
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Base URL for registration page:
Code:
http://localhost/codeigniter-project/users/register

Registration page:
PHP Code:
<?php echo validation_errors('<p class="bg-danger">'); ?>
<?php 
echo form_open('users/register', ['id' => 'register_form''class' => 'form-horizontal']); ?>
    <div class="form-group">
        <?php
        
echo form_label('First name');
        echo form_input([
            'name' => 'first_name',
            'value' => set_value('first_name'),
            'class' => 'form-control',
            'placeholder' => 'Enter first name',
        ]);
        ?>
    </div>

    ... 

Controller:
PHP Code:
class Users extends CI_Controller
{
    public function register()
    {
        $this->form_validation->set_rules('first_name''First name''trim|required|min_length[2]');
        $this->form_validation->set_rules('last_name''Last name''trim|required|min_length[2]');

        if ($this->form_validation->run() == false) {
            return $this->load->view('layouts/main', ['main_view' => 'users/register']);
        }

        ... 

When I open the URL for the very first time there were no validation errors produced.
But if I try to fill the form and for example provide only 'fisrt name' (and do not provide 'last name'), then validation errors generated and displayed on the register page.
So why were no errors genererated in the beginning, when I open the base url for the very first time?
Because you haven't submitted any data. It can't validate something that aren't there.

It works as intendent.
Loading the page the first time is a GET request. Validation only tests the rules when responding to a POST request and a form "submit" is (should be) a POST operation. (Which is just another way to say what @jreklund said.)
Quote:When I open the URL for the very first time there were no validation errors produced.

Validation errors are meant to inform the user that he didn't fill in the form correctly.
Why would you want to show validation errors before the user even made one attempt to fill in the form?
(09-22-2018, 10:39 AM)Wouter60 Wrote: [ -> ]Why would you want to show validation errors before the user even made one attempt to fill in the form?
I just follow the controller code thus it seemed that there should be errors dislayed because validation will false.