Form Validation Callbacks Load From External Source |
[eluser]skunkbad[/eluser]
I've got more than one controller that calls some of the identical callback functions to validate form data using the CI Form Validation class. I'm wondering what I can do to get them out of the controller, and into a centralized form validation callback library, plugin, helper, or whatever. The docs don't mention loading the callbacks from an external source, and I'd be happy if it is possible. What are other people doing to make this happen? Right now I'm trying to test loading one of the callback functions from the controllers constructor. So, for instance: Code: function Administration() Then, elsewhere in the controller, when I'm trying to use the function as a callback for form validation: Code: $this->form_validation->set_rules('uname', 'USERNAME' , 'trim|required|max_length[12]|callback_usernamecheckoff'); and then I have the actual plugin, appropriately placed in system/application/plugins/ and named usernamecheckoff_pi.php: Code: <?php But this isn't working. If for instance, in my form I use a name other than 'billythekid', the form validation error message is not displayed.
[eluser]janogarcia[/eluser]
Take a look at MY Form validation - Simple Callbacks into Models, it will let you specify where to locate each callback: The current Controller or a User specified Model. It defaults to the current Controller, but you can override that for each callback.
[eluser]skunkbad[/eluser]
OK, so I figured out how to make for external form validation callbacks. I'm not sure this is the best way, but it's what works. The key for my success was to use a model, in which I can store all the different callbacks as methods. I know to a MVC purist, I am probably committing the ultimate sin using a model for this purpose, but like I said before, it is what works. Should I have made a library? Well, maybe so. Anyways... The form validation rule is set as normal, then inside the controller I put a method/function like this: Code: public function _usernameChecks($uname){ And the model, which currently only has one method (because it took me so long to figure out what to do to make this work) looks like this: Code: <?php If anyone has any other ideas, let me know.
[eluser]skunkbad[/eluser]
Support this kind of functionality by voting at uservoice! http://codeigniter.uservoice.com/forums/...?ref=title
[eluser]janogarcia[/eluser]
Just added my vote. I can't understand why this hasn't been implemented yet. |
Welcome Guest, Not a member yet? Register Sign In |