Welcome Guest, Not a member yet? Register   Sign In
Form_validation matches rule
#1

[eluser]Unknown[/eluser]
Form validation works fine with array in the name of inputs.
Ie:
Code:
<input name='user[address][city]'>

But the rule: matches don't.

I found in line 583 of Form_validation.php file
Code:
if (preg_match("/(.*?)\[(.*?)\]/", $rule, $match))
It send to method matches the field: user[address
I changed this line to:
Code:
if (preg_match("/(.*?)\[(.*?)\]$/", $rule, $match))
Now It send to method matches the field: user[address][city] and i test in the matches methods with _reduce_array method of Form_validation library.

I made a copy-and-paste of some lines of the Form_validation class to My_Form_validation

Code:
/**
     * Match one field to another
     *
     * @access    public
     * @param    string
     * @param    field
     * @return    bool
     */
    function matches($str, $field)
    {
        //test if $field is array
        if (strpos($field, '[') !== FALSE AND preg_match_all('/\[(.*?)\]/', $field, $matches))
        {    
            $x = explode('[', $field);
            $indexes[] = current($x);

            for ($i = 0; $i < count($matches['0']); $i++)
            {
                if ($matches['1'][$i] != '')
                {
                    $indexes[] = $matches['1'][$i];
                }
            }
            
            $field = $this->_reduce_array($_POST, $indexes);
        }
        else if ( ! isset($_POST[$field]))
        {
            return FALSE;                
        }
        else
        {
            $field = $_POST[$field];    
        }

        return ($str !== $field) ? FALSE : TRUE;
    }

Thanks.




Theme © iAndrew 2016 - Forum software by © MyBB