Custom message for form validation? |
[eluser]yxxm[/eluser]
Hi all, I'm pretty new to CodeIgniter here, so forgive if this question has been answered already. I've looked in the docs for setting a custom error message when a field (form) validation fails. What I see in the documentation (Setting Error Messages) is: Quote:All of the native error messages are located in the following language file: language/english/form_validation_lang.php So I went ahead and changed 'required' to my custom rule. Obviously this worked, but it changed it for all of the fields (as expected). Essentially, I want the default string for the required rule, but for one of my fields I would like an error message that I explicitly define. So, for example, here is what I'm trying to accomplish: Code: // Name is okay with the default required message (as well as the rest of the [omitted] fields) It seems that logically, set_rules should have an optional fourth parameter that allows you to override the default error message for the rule, but keep the rule (behavior) in tact. The only way I see that this can be accomplished is by creating a custom callback that mimics the behavior of required. Am I missing something? Thanks in advance!
[eluser]bretticus[/eluser]
I agree with you. However, rules are supposed to be linked closely to an error message. And even though it's easy to change the error message (config file or explicit set_message call,) doing this changes the error message for the required rule across the board. So yes, either build a custom callback or extend the Form_validation class (if you plan on reusing the rule.) Here is an example of extending (not tested but should work.) Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
[eluser]yxxm[/eluser]
Hey bretticus, Thanks for the response. I appreciate the example you posted. The thing is, I won't be using _just_ the type field. The idea is that for any given field, I may want to specify a custom error message. It just seems silly to me (and that it defies the concept of abstraction) that I would have to write a different validation callback function for each custom message. I was thinking that it would be acceptable to make a rule like max_length[n], where you can specify a parameter (where that parameter would be the error message, in this case), except I couldn't find an example in the documentation of how to go about that. In any event, I tried the example that you posted with limited success. I created the following class: Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); named it 'MY_Form_validation.php', and put it in my /application/libraries folder. Note that the echo statements are in there for testing purposes. I then proceeded to do: Code: $this->load->library('form_validation'); When running this code, I see the 'constructed' message, but it seems like the required_message function never fires. I read over the docs again, thinking maybe I had to add callback_ to the front when using set_rules, but that didn't work either. Any help would be appreciated!
[eluser]WanWizard[/eluser]
The callback_ prefix is only needed if you call a method in your controller, not for methods in the form validation class. Validation methods are only called when there is something to validate. Maybe type[] evaluates to empty?
[eluser]zyddee[/eluser]
Thx, I also found the solution in this thread, by passing the $config variable http://ellislab.com/forums/viewthread/148607/ |
Welcome Guest, Not a member yet? Register Sign In |