Jun 2 2011
CodeIgniter Extending Form_Validation
You can add generic validation functions to codeigniter by extending the Form_Validation. You do this by creating a file called /application/library/MY_Form_Validation.php.
If you load the Form_Validation library it will pick-up this class and use it instead. Because it extends the original class CI_Form_validation you still have access to all the normal functions.
Here is an example that adds 2 validation functions:
= $min);
if ($out == false) {
$this->set_message('greater_or_equal', 'The %s field must be greater or equal to the %s');
}
return $out;
}
public function less_or_equal($str, $max)
{
if ( ! is_numeric($str))
{
return FALSE;
}
if ( ! isset($_POST[$max]))
{
return FALSE;
}
if(! is_numeric($max)) {
if (! is_numeric($_POST[$max])) {
return FALSE;
}
else {
$max = $_POST[$max];
}
}
$out = ($str <= $min);
if ($out == false) {
$this->set_message('greater_or_equal', 'The %s field must be greater or equal to the %s');
}
return $out;
}
}
You can use it like this:
$this->form_validation->set_rules('max', 'maximum', 'numeric|greater_or_equal[min]');