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'. |
Messages In This Thread |
Use Models, Libraries and Helpers in Form Validation rules - by El Forum - 12-22-2011, 11:27 AM
Use Models, Libraries and Helpers in Form Validation rules - by El Forum - 01-26-2012, 08:45 PM
Use Models, Libraries and Helpers in Form Validation rules - by El Forum - 10-15-2012, 04:48 AM
Use Models, Libraries and Helpers in Form Validation rules - by El Forum - 11-28-2012, 05:02 PM
Use Models, Libraries and Helpers in Form Validation rules - by El Forum - 11-28-2012, 05:08 PM
Use Models, Libraries and Helpers in Form Validation rules - by El Forum - 11-29-2012, 07:08 AM
Use Models, Libraries and Helpers in Form Validation rules - by El Forum - 12-02-2012, 01:29 PM
|