Server Side Validation

The simple example below might be used for example on a simple registration form requiring  an email address, and a password. If the data being passed to the controller meets the validation requirements, we redirect to the members page, if the validation fails, we redirect back to the login form.

$this->load->library('form_validation')
$this->form_validation->set_rules('email','EMail','required|valid_email')
$this->form_validation->set_rules('password','Password','required')

if ($this->formvalidation->run()==TRUE) {
     redirect('main/members')
} else {
    $this->load->view('login')
}

we need the form validation library, and that generally would be loaded in the controllers constructor

Validation Options

There’s a whole bunch, and you chain them together, as in the example above

  1. required
  2. numeric
  3. max_length[10]
  4. min_length[5]

Custom Validation

In addition to the standard validation rules provided by code igniter, we can create our own custom callback functions to check for specific user requirements. For example if developing a user registration system we might want to determine if the email, or the username have already been taken.

When we add custom rules through the use of callback functions, we also need to specify the error message that should be displayed. This can be coded directly in the controller, or directly added to the config file (however, the problem with this approach is that this code will possibly be overwritten with updates of code igniter.

Leave a comment