Welcome Guest, Not a member yet? Register   Sign In
Custom Error messages for Form Validation
#1

[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.
#2

[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




Theme © iAndrew 2016 - Forum software by © MyBB