Use Models, Libraries and Helpers in Form Validation rules |
[eluser]CroNiX[/eluser]
The original idea for this came from Skunkbad in this thread, where he created a way to use models in validation rules. I simply took his concept created a single new validation rule that would allow using Models, Libraries and Helpers as well. Additionally, if your own callback rule requires parameters passed to it via square brackets, you can still use those as you originally would with the original form validation class. Please see the notes in the class for reserved characters that cannot be used as delimiters in your own rules or they will clash with CI's rule delimeters or the ones I use. The main benefit is to be able to consolidate and separate the validation logic outside of the controller and also avoid having a huge MY_Form_Validation rule set for larger projects. Code: <?php Use examples: 1) You have a simple rule in a model. /application/models/test_model.php Code: class Test_model extends CI_Model { Code: $this->form_validation->set_rules('field_name', 'Field Name', 'external_callback[model.test_model.not_word]'); external_callback[model.test_model.not_word] Type of external callback Name of file Rule name in file 2) Let's say our rule had additional parameters passed to it. You do it like normal in square brackets. Code: class Test_model extends CI_Model { Code: $this->form_validation->set_rules('field_name', 'Field Name', 'external_callback[model.test_model.not_word[Tuesday]]'); The rest of the code is the same for Libraries and Helpers. You just use the appropriate word instead of "model" in the rules. I hope it's useful and remember skunkbad came up with the concept. If there are any improvements or problems please post them. I haven't done extensive testing. Note: even if you have 10 external callbacks on the same form, the error messages still show up for the correct field even though all error messages are named after 'external_callback'.
[eluser]ajustesen[/eluser]
Ignore my post on the other thread, got this working. Thanks!
[eluser]batfastad[/eluser]
This is an awesome idea. Something that should be available in the core really IMO. It makes sense to keep shared validation callbacks in a helper rather than repeating across many controllers. Question though... if I'm doing this with my callback stored in a helper, how do I set the error message within the helper? Code: $CI =& get_instance(); I get Quote:Unable to access an error message corresponding to your field name. Any ideas? Cheers, B EDIT: Ok everyone go home! The problem was... I was being an idiot!
[eluser]Ashes-to-Ashes[/eluser]
I am running something like this: Code: // in library: It appears to never call the call back function
[eluser]CroNiX[/eluser]
It doesn't look like you are setting your rules right. Code: function set_rules() should be Code: $this->CI->form_validation->set_rules(...);
[eluser]Ashes-to-Ashes[/eluser]
[quote author="CroNiX" date="1354147726"]It doesn't look like you are setting your rules right.[/quote] You are correct. This problem was already fixed in the code. I fixed it in the post as well. Still having the issue though. I have an echo statement in my callback, that never displays. So it appears that the callback is not getting called versus returning true when it shouldn't.
[eluser]Ashes-to-Ashes[/eluser]
Okay, I have debugged this a bit more. The call back is working in the main controller, but if I try to use the model callback it does nothing (no errors or anything, always returns true) |
Welcome Guest, Not a member yet? Register Sign In |