CodeIgniter Forums
Extending Form Validation - 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: Extending Form Validation (/showthread.php?tid=56855)



Extending Form Validation - El Forum - 01-25-2013

[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) {
        return (bool) preg_match('^(0[1-9]|1[012])[/](0[1-9]|[12][0-9]|3[01])[/](19|20)\d\d$^', $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) {
        return (bool) preg_match('^(0[1-9]|1[012])[/](0[1-9]|[12][0-9]|3[01])[/](19|20)\d\d$^', $val);
}


valid_date is never called in MY_Form_Validation unless required is set first. Any suggestions would be greatly appreciated. Thanks.



Extending Form Validation - El Forum - 01-25-2013

[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.


Extending Form Validation - El Forum - 01-28-2013

[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]


Extending Form Validation - El Forum - 01-28-2013

[eluser]PhilTem[/eluser]
How's your class constructor looking like?


Extending Form Validation - El Forum - 01-28-2013

[eluser]ci_user[/eluser]
Code:
class MY_Form_validation extends CI_Form_validation {

    public function __construct()
    {
        parent::__construct();
    }



Extending Form Validation - El Forum - 01-28-2013

[eluser]PhilTem[/eluser]
__construct() must look like this

Code:
public function __construct($rules = array())
{
  parent::__construct($rules);
}

otherwise your rules won't be loaded when the class is initialized thus you cannot use it.


Extending Form Validation - El Forum - 01-28-2013

[eluser]ci_user[/eluser]
Thank you.


Extending Form Validation - El Forum - 01-28-2013

[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).


Extending Form Validation - El Forum - 01-29-2013

[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!