Welcome Guest, Not a member yet? Register   Sign In
problem with form_validation : conditional fields validation (one field depends upon another)
#11

[eluser]t'mo[/eluser]
herringtown, I couldn't do a pure override solution to this, withouth also overriding the core "_execute" method.

But, given how useful conditional validation would be if it were in the core, I thought it worth my time to ask them to put it in. (See also http://ellislab.com/forums/viewthread/110793/ .) Hopefully, the requested change could make it into the core distribution.
#12

[eluser]Mike Ryan[/eluser]
Hi,

I had the same problem - I hacked the form_validation core to include a depends_on function, which does exactly what you want: Just define it in your validation rules like "numeric|depends_on[other_field]".

You do, unfortunately, need to hack at some core files - I'll rewrite this as an extension if there is any interest. I'm kind of hoping something similar will be incorporated into the next version of CI, as I have found it quite a useful rule to have.
#13

[eluser]Unknown[/eluser]
FYI the problem is that the parameter is stripped here in Form_Validation.php line 487 CI v1.7.1:

Code:
// If the field is blank, but NOT required, no further tests are necessary
        $callback = FALSE;
        if ( ! in_array('required', $rules) AND is_null($postdata))
        {
            // Before we bail out, does the rule contain a callback?
            if (preg_match("/(callback_\w+)/", implode(' ', $rules), $match))
            {
                $callback = TRUE;
                $rules = (array('1' => $match[1]));
            }
            else
            {
                return;
            }
        }

\w will stop as soon as it hits the [] resulting in just the callback name as a match.
#14

[eluser]Unknown[/eluser]
[quote author="juven14" date="1243156740"]FYI the problem is that the parameter is stripped here in Form_Validation.php line 487 CI v1.7.1:

Code:
// If the field is blank, but NOT required, no further tests are necessary
        $callback = FALSE;
        if ( ! in_array('required', $rules) AND is_null($postdata))
        {
            // Before we bail out, does the rule contain a callback?
            if (preg_match("/(callback_\w+)/", implode(' ', $rules), $match))
            {
                $callback = TRUE;
                $rules = (array('1' => $match[1]));
            }
            else
            {
                return;
            }
        }

\w will stop as soon as it hits the [] resulting in just the callback name as a match.[/quote]


You can fix this bug in Form_Validation.php library with this code :

Code:
// If the field is blank, but NOT required, no further tests are necessary
        $callback = FALSE;
        if ( ! in_array('required', $rules) AND is_null($postdata))
        {
            // Before we bail out, does the rule contain a callback?
            if (preg_match("/(callback_.*?\[(.*?)\])/", implode(' ', $rules), $match))
            {
                $callback = TRUE;
                $rules = (array('1' => $match[1]));
                $params = (array('1' => $match[2]));
            }
            else
            {
                return;
            }
        }
#15

[eluser]jpi[/eluser]
[quote author="cpp643" date="1237252627"]in controller check either of those fields by

if($this->input->post('cmb_club')==idofother)
{
set rules and fields validation for other club input field.
}
else
{
set rules and fields validation for club drop down.
}[/quote]

Set_rules doesn't work (they are ignored) if you use config file for form_validation.




Theme © iAndrew 2016 - Forum software by © MyBB