I’m setting the same callback function for multiple fields in my form:
foreach($fields as $class => $i)
{
++$i;
$validation->set_rules('car_' . $i . '_one', $class, 'trim|xss_clean|callback_validate_car_fields[TRUE]');
$validation->set_rules('car_' . $i . '_twor', $class, 'trim|xss_clean|callback_validate_car_fields[FALSE]');
$validation->set_rules('in_car_' . $i . '_one', $class, 'trim|xss_clean|callback_validate_car_fields[TRUE]');
$validation->set_rules('in_car_' . $i . '_two', $class, 'trim|xss_clean|callback_validate_car_fields[FALSE]');
}
(class variale is the Car in this case)
Here is the callback function:
function validate_car_fields($input, $required)
{
if ($required === "TRUE" && empty($input))
{
$this->form_validation->set_message('validate_car_fields', 'Field %s can not be empty!');
return FALSE;
}
return TRUE;
}
And it is showing the doubled errors:
* Field Car can not be empty!
* Field Car can not be empty!
* Field Car can not be empty!
* Field Car can not be empty!
It is understandable behaviour in this case, but I would like to show just one error per field name, instead of 4 for each field. How this can be done?
Try this