How validation works and its callback function in Code Igniter


Validation framework in the Code Igniter is pretty powerful, i have to admit that once you get around to use it, eventually it can save lots of web development time, and it's much more secure rather than building it up yourself. CI provides a number of commonly used validation rules such as "required fields", "numeric fields", "alpha fields", and many others. Additionally, you can build up your own custom validation function by using callback. A comprehensive user guide from Code Igniter in regard to form validation is available here.

Notice: This Code Igniter is using version 1.6.3, there is currently a much newer version 1.7.2, and it's using a new form validation class instead, just a warning that the below code is dedicated solely for CI version 1.6.3 and below.

The sample code below will elaborate how CI validation works around the clock:

  1. /* Validation method for current, new and confirm password field */
  2. function _set_validation_password($adminid)
  3. {
  4. $rules['c_password'] = "required|min_length[6]|callback__password_check[$adminid]";
  5. $rules['n_password'] = "required|min_length[6]|matches[passconf]";
  6. $rules['passconf'] = "required";
  7. $this->validation->set_rules($rules);
  8.  
  9. $fields['c_password'] = 'Current Password';
  10. $fields['n_password'] = 'New Password';
  11. $fields['passconf'] = 'Confirm Password';
  12. $this->validation->set_fields($fields);
  13. $this->validation->set_error_delimiters('<span style="color: teal; font-size: 11px;">', '</span>')<span style="color: teal; font-size: 11px;">&nbsp;</span>;
  14. }

A couple of things to note:

"required", "min_length[x]" are default validation rules from the Code Igniter framework.
callback__password_check is a custom validation function to accomodate your own need.
$rules should be filled with your preferred validation rules.
$fields should be filled with field name what should come up on validation error message.
$this->validation->set_error_delimiters will enclose the validation error message with your custom css.
the underspace symbol (_) in front of function name means the function is private and cannot be called from the URL address alone.
the callback function always has to be in "callback_" format with underspace symbol at the end.

 

  1. /* Check into database whether the input for current password is valid or not */
  2. function _password_check($current_password, $adminid)
  3. {
  4. $result = $this->oAdmin->check_password($current_password, $adminid);
  5.  
  6. if($result->num_rows() == 0)
  7. {
  8. $this->validation->set_message('_password_check', 'Current password is invalid');
  9. return FALSE;
  10. }
  11. else
  12. {
  13. return TRUE;
  14. }
  15. }

A couple of things to note:

$this->oAdmin->check_password is a model function to check the database to ensure whether the current user password is valid or not.
$this->validation->set_message will set your error message when the validation fails.

 

  1. function change_password()
  2. {
  3. $this->_set_validation_password($this->session->userdata('adminid'));
  4. if($this->validation->run() == FALSE)
  5. {
  6. // do something if the validation doesn't pass
  7. }
  8. else
  9. {
  10. // do something if the validation passes
  11. }
  12. }

A couple of things to note:

$this->_set_validation_password will set the following validation function as default.
$this->validation->run() will execute the form validation function set as above.