![]() |
Custom validation error messages per rule - 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 validation error messages per rule (/showthread.php?tid=10816) |
Custom validation error messages per rule - El Forum - 08-20-2008 [eluser]Khoa[/eluser] Is it a bit redundant when we need both the function and the rule for each checking to work? Can we make it work with just the $lang['required_custom'] itself but no need for the function required_custom($str)? Custom validation error messages per rule - El Forum - 08-20-2008 [eluser]xwero[/eluser] the line is related to the method. If there is no method the line is fat for the language file. You can go without the line but then the easiest way is to put the string in the set_message method. This is less flexible as you hard code the string then. You can create another language file to put your custom lines in en load it when you need to use the set_message method. I wonder why you find it so strange that you need a function and a language line because that is how the CI default rules work as well. Custom validation error messages per rule - El Forum - 08-20-2008 [eluser]Khoa[/eluser] Ye, I know that inside the builtin validation class, there is 1 function for each of the rule like function required, function max_length...which is reasonable because this is the first time such rule is set up. So we definitely need to have a place to say: how to check for "required", how to check for "max_length", etc. But when we set up our custom rules, that's differernt story. The rules are there, we just call it, but instead of returning the default error message, we want it to return our custom message. So, what I'm THINKING (just thinking, not coding yet :-P) is to have sth like this: Code: $rules['title'] = 'customValidate[required,title]|customValidate[max_length[100],title]'; On the line of $rules['title'] =... we can replace the customValidate by sth else to indicate that this is our custom function but not the builtin on, sth like: _max_length[100, title], and inside the code we check if the function starts with "_", we will redirect it to the customValidate function. This helps reduce the amount of typing but still giving the function a meaningful name inside the validation class. The main advantage of this (well, if it ever works!) is when we have another field to validate, we just need to add another line into the lang file saying $lang['required']['newField'] = ... and write $rules['newField'] = _required[newField]. Code: $rules['title'] = '_required[title]|_max_length[100,title]'; This is just a prototype anyway :-) Custom validation error messages per rule - El Forum - 08-20-2008 [eluser]xwero[/eluser] Now you are talking about an better/easier way to set custom messages. I would even push it further and reserve the first parameter of every rule, even callbacks for a custom message. Code: $rules['field1'] = 'max_length[,100]'; // no custom message This is possible because the rule is split from the params before the rule is called. Custom validation error messages per rule - El Forum - 08-20-2008 [eluser]Khoa[/eluser] Ye, that works for me too. All I think important is: able and easy to add custom message with least code and least customization of the builtin class. If you put the custom message as a parameter, does it mean you have to customize every built-in function such as required($str), max_length($str) and so on to make sure that the code will take the parameters starting from position 1 instead of 0? I know this is not a big deal (isn't it?) but then what happens when we upgrade to the next version of CI? Please advise. Custom validation error messages per rule - El Forum - 08-20-2008 [eluser]xwero[/eluser] You don't have to overwrite the rules to do what i propose. You only need to change the run method. Where the parameters are fetched you need to check if a custom message is added or not. If a custom message is added you need to check if the line exists and if it does display it. I would advise against setting the custom message as second parameter because then you have to do things like Code: $rules['field4'] = 'required[,custom_msg]'; As far as updating CI is concerned if you put your code in the MY_Validation.php file you can wait as long as you want to use your solution. Custom validation error messages per rule - El Forum - 08-20-2008 [eluser]Khoa[/eluser] Hmm. I see. I will give it a go, and will share it here when I have it work. Thanks a lot for all your time and patience xwero ;-) |