CodeIgniter Forums
Form_validation matches rule - 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: Form_validation matches rule (/showthread.php?tid=15781)



Form_validation matches rule - El Forum - 02-14-2009

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