![]() |
Extend form_validation to add custom error messages - 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: Extend form_validation to add custom error messages (/showthread.php?tid=40029) |
Extend form_validation to add custom error messages - El Forum - 03-28-2011 [eluser]Exangelus[/eluser] Hi all, I'm currently building an framework within CI to easily maintain all sorts of forms. For this purpose we're making a library that utilizes the excellent FormGen library from Frank Michel and the Livevalidation js class from Alec hill. Now I'd like to extend CI's Form_validation class because we like to set custom error messages for each field. Therefore I tend to extend the _execute function and add some functinality to replace set error messages for custom one's. Here's the code: (I'll add a simple function to set the custom messages) Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); We willfully cancel the option for multiple error's for one field for our project's purpose ![]() Now something is going wrong, the form always validates false. I'm not quit sure how the return value of _execute influences this but I have tried most logical options (true, false, empty array, nothing) but it always validates false.. Thanks in advance! Extend form_validation to add custom error messages - El Forum - 03-28-2011 [eluser]InsiteFX[/eluser] Your missing the Constructor unless you left it out of the code! InsiteFX Extend form_validation to add custom error messages - El Forum - 03-28-2011 [eluser]Exangelus[/eluser] Completely forgotten, thanks!!! Edit: unfortunaly this doesn't solve the problem, still always getting getting failed form validation.. anyone further idea's? Extend form_validation to add custom error messages - El Forum - 03-28-2011 [eluser]InsiteFX[/eluser] Why don't you just use this method? Code: // -------------------------------------------------------------------- InsiteFX Extend form_validation to add custom error messages - El Forum - 03-28-2011 [eluser]Exangelus[/eluser] Well it let's you set a custom error for a specific rule, not for a specific field and that's the functionality we want. Extend form_validation to add custom error messages - El Forum - 03-28-2011 [eluser]InsiteFX[/eluser] Each Rule is set to a specific field and each field has a specific Rule! Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); InsiteFX Extend form_validation to add custom error messages - El Forum - 03-29-2011 [eluser]Exangelus[/eluser] Not entirely true I think, the syntax to call set_message is: Code: $this->form_validation->set_message('rule', 'Error Message'); You can only specify a rule, not the field that specific rule would apply to as you explain. Also relocating the call to parent doesn't work unfortunaly. I tried to copy the contents of the complete _execute function and put them in place of the call to parent and then it works. So one way or the other it has something to do with the inherintance of the parent's function.. already tried removing the _ prefix of _execute (everywhere in the lib!) but this doesn't change a thing neither.. Extend form_validation to add custom error messages - El Forum - 04-17-2011 [eluser]niranjnn01[/eluser] Hello, I too had problem with being able to specify a custom error message to a "rule", but not a "field". I solved it as follows(at least to my needs) I had two fields country and state in drop down box, and they were mandatory fields. using this.. $this->form_validation->set_message('is_natural_no_zero', 'The %s field is required'); will give two messages "The Country field is required" "The State field is required" Hope it helps someone. Regards Rakesh |