Blog Codeigniter Recovered iNextrix

Custom form validation in CodeIgniter

In any website the main thing for form is validation. There are many ways for validation but one of most secure way it controller-side validation (server-side). CodeIgniter is having that feature available in-built. There are some basic steps to implement it but it won’t have all validation available. So in this blog I will explain how we can add custom validation.
Lets take example of I want to compare two dates:
For this we need to add function in system/libraries/Form_validation.php.

public function greater_than_date($str, $min)
{ $date1=strtotime($str);
return $date1 > $date2;
Now, we need to set custom message which need to show when validation is fire. For this we need to add message in system/language/english/form_validation_lang.php like following:
$lang['greater_than_date']= "The %s must contain a date greater than %s.";Here, We need to be more careful about the name of array. The array name must $lang and key name should same as function name.
If your website is using any other language than english then we need to make changes in that folder instead of in English folder.

Leave a comment