Sep 24 2011
CodeIgniter Hack Form_Validation Callbacks
When im writting a Codeigniter 2 application I prefer to have most of my code in the libraries and leave the controller as blank as possible. This allows me to reuse code like forms in other sections of the site. eg you want the login for to be in 2 different places. However Codeigniter’s Form_valiation class does not allow for custom validation callbacks in a library. The only way to fix this is to hack the Form_validation class. I have created a modification that allows you to call a validation callback from any class.
This hack starts at line 585 of the /System/Libraries/Form_validation.php.
// Call the function that corresponds to the rule
if ($callback === TRUE)
{
// custom start //
$role_comp = explode('->', $rule);
$class = $role_comp[0];
$method = $role_comp[1];
if ( ! method_exists($class, $method))
{
continue;
}
// Run the function and grab the result
$result = $this->CI->$class->$method($postdata, $param);
// custom end //
You use it with the normal callback_ but then add in the class name -> then your callback name.
$this->form_validation->set_rules('email', 'Email', 'callback_libusers->check_email_status');