![]() |
Form validation error message for length validations - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Form validation error message for length validations (/showthread.php?tid=14685) |
Form validation error message for length validations - El Forum - 01-11-2009 [eluser]helmutbjorg[/eluser] I'm using the form_validation library to test for min_length, max_length, and exact_length and the following error messages are being returned... Code: The title field must be at least 200 characters in length. This is good. But not great. It is better in these sort of error messages to give the user some ideas of how they could fix the problem. Like so... Code: The title field must be at least 200 characters in length. You have 140 characters. Now what would be the best way of getting that result? The way error messages are constructed in the form_validation library only allows for max of two '%s' sprintf params. Form validation error message for length validations - El Forum - 01-12-2009 [eluser]helmutbjorg[/eluser] Okay... i figured out a solution to the problem. Thought i'd share it with you guys. Extend the form_validation library by creating the following file: system/application/libraries/MY_Form_validation.php Code: class MY_Form_validation extends CI_Form_validation { And then you create a custom language file: system/application/language/english/MY_form_validation_lang.php Code: $lang['my_min_length'] = "The %s field must be at least %s characters in length. You have [count] characters."; Then in your code you can use the following... Code: $this->form_validation->set_rules('title', 'title', 'trim|required|my_max_length[20]|xss_clean'); Cheers! Form validation error message for length validations - El Forum - 01-15-2009 [eluser]helmutbjorg[/eluser] On a follow up note, you can do it a better way like so... Code: // Custom max length This way all of your existing code will still work! You can still use max_length (instead of my_max_length) in your validation lists. But the newer (more helpful) error message will show too!! Form validation error message for length validations - El Forum - 12-03-2010 [eluser]aprium[/eluser] Thanks, found this really handy. Form validation error message for length validations - El Forum - 04-23-2013 [eluser]jonnny[/eluser] A slight tweak makes it work with the set_message() function if you cant use the lang file. I wanted generic messages, and %s would put the field name in, not the max length that I wanted. Code: function max_length($str, $val) Form validation error message for length validations - El Forum - 05-02-2013 [eluser]jonnny[/eluser] Even better: Code: function max_length($str, $val) I was occasionally getting errors because $this->_error_messages['max_length'] did not exist (the times when I had not manually changed the error message in advance). This fixes that. |