• strict warning: Non-static method view::load() should not be called statically in /home1/eangkasa/public_html/sites/all/modules/views/views.module on line 879.
  • strict warning: Declaration of views_handler_argument::init() should be compatible with views_handler::init(&$view, $options) in /home1/eangkasa/public_html/sites/all/modules/views/handlers/views_handler_argument.inc on line 745.
  • strict warning: Declaration of views_handler_filter::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /home1/eangkasa/public_html/sites/all/modules/views/handlers/views_handler_filter.inc on line 589.
  • strict warning: Declaration of views_handler_filter::options_submit() should be compatible with views_handler::options_submit($form, &$form_state) in /home1/eangkasa/public_html/sites/all/modules/views/handlers/views_handler_filter.inc on line 589.
  • strict warning: Declaration of views_handler_filter_node_status::operator_form() should be compatible with views_handler_filter::operator_form(&$form, &$form_state) in /home1/eangkasa/public_html/sites/all/modules/views/modules/node/views_handler_filter_node_status.inc on line 14.
  • strict warning: Non-static method view::load() should not be called statically in /home1/eangkasa/public_html/sites/all/modules/views/views.module on line 879.
  • warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /home1/eangkasa/public_html/sites/all/modules/counter/counter.module on line 136.
  • warning: strtotime(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /home1/eangkasa/public_html/sites/all/modules/counter/counter.module on line 159.
  • warning: strtotime(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /home1/eangkasa/public_html/sites/all/modules/counter/counter.module on line 159.
  • strict warning: Non-static method view::load() should not be called statically in /home1/eangkasa/public_html/sites/all/modules/views/views.module on line 879.
  • strict warning: Declaration of views_handler_field_comment::init() should be compatible with views_handler_field::init(&$view, $options) in /home1/eangkasa/public_html/sites/all/modules/views/modules/comment/views_handler_field_comment.inc on line 50.
  • strict warning: Declaration of views_handler_field_comment_username::init() should be compatible with views_handler_field::init(&$view, $options) in /home1/eangkasa/public_html/sites/all/modules/views/modules/comment/views_handler_field_comment_username.inc on line 48.
  • strict warning: Declaration of views_plugin_row::options_validate() should be compatible with views_plugin::options_validate(&$form, &$form_state) in /home1/eangkasa/public_html/sites/all/modules/views/plugins/views_plugin_row.inc on line 135.
  • strict warning: Declaration of views_plugin_row::options_submit() should be compatible with views_plugin::options_submit(&$form, &$form_state) in /home1/eangkasa/public_html/sites/all/modules/views/plugins/views_plugin_row.inc on line 135.
  • strict warning: Non-static method view::load() should not be called statically in /home1/eangkasa/public_html/sites/all/modules/views/views.module on line 879.
  • strict warning: Non-static method view::load() should not be called statically in /home1/eangkasa/public_html/sites/all/modules/views/views.module on line 879.

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.