Extending Form Validation |
[eluser]ci_user[/eluser]
Custom validations working but only if they are secondary to a core validation... so while this works: In the controller: Code: $this->form_validation->set_rules('dob', 'D.O.B.', 'required|valid_date'); And then in MY_Form_Validation.php Code: public function valid_date($val) { This does NOT work: In the controller: Code: $this->form_validation->set_rules('dob', 'D.O.B.', 'valid_date'); And then in MY_Form_Validation.php Code: public function valid_date($val) { valid_date is never called in MY_Form_Validation unless required is set first. Any suggestions would be greatly appreciated. Thanks.
[eluser]Aken[/eluser]
First, your file and class name should not have a capitalized V. It should be MY_Form_validation. Second, there is logic in the class that says "If the field is blank, but NOT required, no further tests are necessary." So if you are testing it with a blank value, your rule won't fire. If you add a value, it should work. This is standard functionality of the library.
[eluser]ci_user[/eluser]
Thank you for your response. I am not testing with a blank value. I want to validate that IF there is a value (though not required) that it is a valid format - but the valid_date function will only run if the field is required. It's frustrating that it works fine if I include the function as a callback in the controller: Code: $this->form_validation->set_rules('dob', 'D.O.B.', 'callback_valid_date'); That seems inefficient. Also, I have changed the lowercase "V" in the file and class name, although all the other required custom validations work fine with the uppercase. [quote author="Aken" date="1359151832"]First, your file and class name should not have a capitalized V. It should be MY_Form_validation. Second, there is logic in the class that says "If the field is blank, but NOT required, no further tests are necessary." So if you are testing it with a blank value, your rule won't fire. If you add a value, it should work. This is standard functionality of the library.[/quote]
[eluser]ci_user[/eluser]
Code: class MY_Form_validation extends CI_Form_validation {
[eluser]PhilTem[/eluser]
__construct() must look like this Code: public function __construct($rules = array()) otherwise your rules won't be loaded when the class is initialized thus you cannot use it.
[eluser]Aken[/eluser]
Changing the constructor shouldn't have any impact on this. The $rules parameter will only contain rules from a config file, not from anything passed to set_rules(). Also, if your constructor doesn't have anything new/custom in it, you can exclude it entirely. I just set up a really bare bones example of what you're trying to do and it works fine. http://pastebin.com/ZFF76ARS Empty field = success, any value in the field = fail (function called fine). If you're using something other than 2.1.3, maybe there's a bug that has since been fixed. I don't know for sure. It could be a problem with your regular expression, or something else in your code (you only posted snippets so I can't say for sure what else you have going on).
[eluser]ci_user[/eluser]
Yes, you are correct. Changing the constructor had no impact. It appears that another custom validation (required_if: that didn't work) function I found online was causing the problem. Once I removed it, everything was fine. Thanks again for your help! |
Welcome Guest, Not a member yet? Register Sign In |