CodeIgniter Forums
Custom Error messages for Form Validation - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Custom Error messages for Form Validation (/showthread.php?tid=36371)



Custom Error messages for Form Validation - El Forum - 11-29-2010

[eluser]Thimuth[/eluser]
Hi,
I need to set custom error messages in my form validations and i did it as follows.

eg:

Code:
$rules['Username'] = "trim|alpha_numeric";
$this->validation->set_message('alpha_numeric',' Username should contain only letters and numbers');

$rules['Password'] = "trim|alpha_numeric";
$this->validation->set_message('alpha_numeric',' Password should contain only letters and numbers.Please resend Password.');

$this->validation->set_rules($rules);

$fields['Username'] = "Username";
$fields['Password'] = "Password";

$this->validation->set_fields($fields);

But in here, when there's an alpha_numeric error with the Username field, it returns the error defined for the password field.
It seems Username's error had been overwritten by the Password's error.

Need a solution to define custom messages for same error validation for multiple field.

Thanks.


Custom Error messages for Form Validation - El Forum - 11-29-2010

[eluser]crikey[/eluser]
The set_message() function will set the message for that rule for all fields that use that rule (so in your example it's using the last set_message() text for all fields with the alpha_numeric rule, giving you the Password text on your Username rule).

Try replacing 'Username' and 'Password' in the text for set_message with %s as per the 'Setting Error Messages' section in the Form Validation user guide.

Cheers,
Grant