Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Validation Identical form passwords

Hi,

I know I saw this either in the forum or in the docs but I just can't seem to find it anymore. There was an example of validating two password fields, the password and the repeated password using an Identical Validator.

Could someone please share the link below or possibly give an example of this in practice?

I have this code:

        // New Password
        $newPassword = new Password('newPassword');
        $newPassword->setLabel('New password');
        $newPassword->setAttribute("placeholder","Password");
        $newPassword->addValidator(new StringLength([
                'min'               => 10,
                'max'               => 45,
                'messageMaximum'    => 'Password is too long, max 45 characters',
                'messageMinimum'    => 'Password is too short, min 10 characters'
            ])
        );
        $this->add($newPassword);

        // Repeat Password
        $repeatPassword = new Password('repeatPassword');
        $repeatPassword->setLabel('Repeat new password');
        $repeatPassword->setAttribute("placeholder","Password");
        $repeatPassword->addValidator(new Identical([
                $newPassword->getValue() => $repeatPassword->getValue(), // <- Doesn't work
                'message' => 'Passwords must match'
            ])
        );
        $this->add($repeatPassword);

The code above doesn't seem to work as I'm not sure how to get the value from the one to compare to the other.



1.7k
Accepted
answer
edited May '15

The code block

$repeatPassword->addValidator(new Identical([
        $newPassword->getValue() => $repeatPassword->getValue(), // <- Doesn't work
        'message' => 'Passwords must match'
    ])
);

Should be

$repeatPassword->addValidator(new Identical([
        'value' =>  $newPassword->getValue(),
        'message' => 'Passwords must match'
    ])
);

This might help you

http://stackoverflow.com/questions/23442830/phalcon-php-form-and-model-validation

and this

http://docs.phalconphp.com/en/latest/api/Phalcon%5Validation%5Validator%5Identical.html