How do you set an error message for max_length
and min_length
rules. For instance, if I set a rule max_length[6]
, I'd like the error message to display
Max characters allowed: 5
views:
392answers:
3
A:
CodeIgniter has one of the better documentations out of all the frameworks. Read the userguide on their site or the one in your CI directory.
http://codeigniter.com/user_guide/libraries/form_validation.html#settingerrors
CD Sanchez
2009-11-08 20:33:00
I did, no help there
gAMBOOKa
2009-11-08 20:33:19
A:
@Daniel is correct. Per CodeIgniter's documentation, you can override the default error message for any validation rule (such as "min_length", "max_length", etc.) like this:
$this->form_validation->set_message('validation_rule', 'Your message here');
So, in your example you could do:
$this->form_validation->set_message('max_length', 'Max characters allowed: 5');
Simply include that where your validation rules exist.
Colin
2009-11-08 23:02:42
I know to override the default message but how do I incorporate the length parameter in the new message? 5 in this case. I have multiple fields with varying lengths, I would prefer not to write a message for each of them.
gAMBOOKa
2009-11-09 00:18:48
+1
A:
gAMBOOKa,
create a 'new' rule that also checks for max_length
$this->form_validation->set_rules('username', 'Username', 'required|_max_length[12]');
and for the method..
function _max_length($val)
{
if (strlen($this->input->post('username')) > $val)
{
$this->form_validation->set_message('_max_length', 'Max characters allowed: 5')
return FALSE;
}
return TRUE;
}
add this to your controller as a new rule and set the message like so ---^
Thorpe Obazee
2009-11-09 01:20:37