• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[1.7.2] form_validation matches[] multidimensional array

#1
[eluser]Unknown[/eluser]
This is the return from the $this->CI->form_validation->_config_rules var. The issue is here... matches[user_change_password[password]]

Code:
Array
(
    [user_change_password] => Array
        (
            [0] => Array
                (
                    [field] => user_change_password[old_password]
                    [label] => Old Password
                    [rules] => required|callback__password_match
                )

            [1] => Array
                (
                    [field] => user_change_password[password]
                    [label] => New Password
                    [rules] => required
                )

            [2] => Array
                (
                    [field] => user_change_password[password_confirm]
                    [label] => Confirm New Password
                    [rules] => required|matches[user_change_password[password]]
                )

            [3] => Array
                (
                    [field] => user_change_password[submit]
                    [label] =>
                    [rules] =>
                )

        )

)

I have been using the matches validation for a while, however this is the first instance I use a multidem array.

I get the following UI error...

Quote:The Confirm New Password field does not match the user_change_password[password field.

Looks like it cuts the multidem bracket (missing "]") to gather the field name in the "matches[]" validation.

I suspect this is a bug since multidem array names work with everything else in the form_validation class.

I expect the validation class to return user_change_password[password] and NOT user_change_password[password from matches[user_change_password[password]]

#2
[eluser]Unknown[/eluser]
Reemplaza la función matches de la clase CI_Form_validation de la siguiente forma:

function matches($str, $field)
{
$x = explode("[", $field);

if ( ! isset($_POST[$x[0]]))
{
return FALSE;
}

$field = $_POST[$x[0]];

if(isset($x[1]))
$field = $field[$x[1]];


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


The problem is in the POST where you use a array, my fast solution is this, but not is not good.

If you know any solution, do not forget to comment.

Sorry, my english is not very good... jeje

#3
[eluser]bilawal360[/eluser]
I've come under this as a problem. The only fix for this right now is to make another $_POST input without the [] and insert that into the form validation rules.

An example in your case would be the following:
Code:
$_POST['user_post_password_tmp'] = @$_POST['user_change_password']['password'];

And then in your validation rules:
Code:
required|matches[user_post_password_tmp]

#4
[eluser]Canimus[/eluser]
I have found the same problem when having this form

Code:
<?php echo form_label(lang('label_password'), 'user[password]'); ?>
            <?php echo form_password('user[password]', set_value('user[password]')); ?>
            
            <?php echo form_label(lang('label_password_confirm'), 'user[password_confirm]'); ?>
            <?php echo form_password('user[password_confirm]', set_value('user[password_confirm]')); ?>

This is the modification in the system/libraries/Form_validation.php

Code:
function matches($str, $field)
    {
        
        // Modification of matches array
        $pattern = '/^([a-z_]+)\[(.*)\]$/';
        preg_match($pattern, $field, $matches);

        if (count($matches) > 2) {
            
            
            if ( ! isset($_POST[$matches[1]][$matches[2]]))
            {
                return FALSE;
            }
            
            $field = $_POST[$matches[1]][$matches[2]];

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

        
        if ( ! isset($_POST[$field]))
        {
            return FALSE;
        }

        $field = $_POST[$field];

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

That help me to overcome the arrays in posts.
This solution will not work with multidimensional arrays!.

Hope this works for you!
Canimus#12

#5
[eluser]ssyberg[/eluser]
The subject of this post is "is this a bug?". The answer is yes and it should be submitted as such.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.