![]() |
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-13-2008 [eluser]Ollie Rattue[/eluser] Hey, I am trying to replace the default validation rule error messages. I have changed some in languages/english/validation_lang.php but I need more specific things for certain forms so I am using if statements. I use a piece of code like this: Code: if( $this->validation->run() == FALSE ): // validation hasn't been passed Which works fine but I run into an issue if I have more than 2 rules on a field. The rules are setup like this in validation_lang.php. $lang['required'] = "The %s field is required."; $lang['matches'] = "Ooops! The passwords you entered do not match."; So what I thought I could do is instead of running the if statement to catch all rules I would just catch the required error e.g. Code: if ($this->validation->password_error == "The password field is required.") { For clarity the setup of this looks like: $rules['password'] = "trim|required|xss_clean|matches[passwordconfirm]"; $fields['password'] = "password"; The text "The password field is required." is exactly what is printed out using <?=$this->validation->password_error;?> when i submit the form when and the password field is blank and I don't run any if statements. So I can't work out why my if statement doesn't pass as true. I thought that maybe it is due to the %s not being set yet. But I have run this within the view file (not good form) and it still doesn't pass as true. It has really got me stratching my head. Any ideas why this woudn't work? Custom validation error messages per rule - El Forum - 08-13-2008 [eluser]plainas[/eluser] that was a bit confusing. Anyway... Create your own validation function instead. With a few ifs set the error message acordingly and return false right after, then run your validation using a callback. Just like it is in the user guide. Custom validation error messages per rule - El Forum - 08-13-2008 [eluser]ontguy[/eluser] I think the Validation class has a function to do what you have in mind. This would let you customize the error message for the rules you'd want changed: Code: $this->validation->set_message('postcode', 'Error Message'); Custom validation error messages per rule - El Forum - 08-14-2008 [eluser]Ollie Rattue[/eluser] [quote author="ontguy" date="1218695518"]I think the Validation class has a function to do what you have in mind. This would let you customize the error message for the rules you'd want changed: Code: $this->validation->set_message('postcode', 'Error Message'); Hey ontguy. With this bit of code I can only have one error message, not different error messages depending on the rule which has not been met. i need something like Code: $this->validation->set_message('password','required', 'Please enter a password') Sadly it looks like this might not exist, and it doesn't seem worth the time to start modifying the validation class. P.S Sorry for the typo where I had postcode instead of password, it confused things a little. I have edited my initial post but this would explain why ontguy has postcode in his code snippet. Custom validation error messages per rule - El Forum - 08-14-2008 [eluser]Andrew A.[/eluser] Hey Earthlion, I was actually having the same issue earlier today, and I resolved it with a setup similar to what you had, however instead in my controller i had: Code: if (!$this->validation->run()) With the isset check, everything went fine. In your example, PHP was checking to see if $this->validation->field_error = TRUE, which is not the case. Custom validation error messages per rule - El Forum - 08-14-2008 [eluser]codex[/eluser] [quote author="EarthLion" date="1218727824"][quote author="ontguy" date="1218695518"]I think the Validation class has a function to do what you have in mind. This would let you customize the error message for the rules you'd want changed: Code: $this->validation->set_message('postcode', 'Error Message'); Hey ontguy. With this bit of code I can only have one error message, not different error messages depending on the rule which has not been met. i need something like Code: $this->validation->set_message('password','required', 'Please enter a password') Sadly it looks like this might not exist, and it doesn't seem worth the time to start modifying the validation class. P.S Sorry for the typo where I had postcode instead of password, it confused things a little. I have edited my initial post but this would explain why ontguy has postcode in his code snippet.[/quote] I think I have done exactly what you need. If you want I can post it for you. Basically what I did was extend the Validation class (MY_Validation) to allow me to do stuff like: Code: $lang['default']['required'] = "This is a required field!"; The first bracket corresponds with the fieldname. If no custom message is set, the default message is shown. Custom validation error messages per rule - El Forum - 08-14-2008 [eluser]codex[/eluser] The class in parts: Code: class MY_Validation extends CI_Validation{ Custom validation error messages per rule - El Forum - 08-14-2008 [eluser]codex[/eluser] Second part: Code: /** Custom validation error messages per rule - El Forum - 08-14-2008 [eluser]codex[/eluser] Last part: Code: function run() Custom validation error messages per rule - El Forum - 08-18-2008 [eluser]Khoa[/eluser] Hi codex, this is exactly what I'm looking for. But can you just upload the lib as a whole so that people can download, and can you also give an example of how it can be used? :-P Sorry that I'm a bit lazy, just want the quickest way to make the thing work. Thanks. |