How can I create my own validation rule in codeigniter 2.x, which can be widely used throughout the application?
I know that we can create callback functions in the controller, which can then be used in the validation rule as -
$this->form_validation->set_rules('user_dob', 'Date of Birth', 'required|callback_validDate|callback_validAge');
And now we can create a check function in the controller as -
public function validDate($date) {
$d = DateTime::createFromFormat('d-M-Y', $date);
if ($d && $d->format('d-M-Y') == $date)
return TRUE;
$this->form_validation->set_message('validDate', ' %s is not in correct date format');
return FALSE;
}
But there is a limitation. I can use this method only inside this particular controller. This function cannot be used for other controllers. I will have to write the same code again.
To do this, I tried to create an auxiliary file with this check function, but again, no luck.
So, how can I reuse the validation function created once in a common file in codeigniter?