Php – validation max_length, min_length in codeigniter

codeigniterPHPvalidation

I am getting an error when trying to set custom validation messages in CodeIgniter for the min_length and max_length validation constraints.

These are my validation rules:

$this->form_validation->set_rules('username', 'Username', 'required|xss_clean|min_length[6]|max_length[10]');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[8]|max_length[20]';

These are my custom validation message:

$this->form_validation->set_message('min_length[3]', '%s: the minimum of characters is three');
$this->form_validation->set_message('max_length[20]', '%s:the maximum of characters is 20');

In my example we have two fields, but I have many fields with different minimum and maximum length values. They all have a specific constraint validations. This messages doesn't work. The message appears by default. Thanks for your help.

Best Answer

Just add another %s to get the length value..

$this->form_validation->set_message('min_length', '%s: the minimum of characters is %s');

You will give the size only if you use %s second time. first time it will give field name you specified..

Related Topic