We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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.



6.6k
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

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

and this

https://docs.phalcon.io/en/latest/api/Phalcon_Validation_Validator_Identical.html

edited Jul '19

I'm getting null when ask about $newPassword->getValue(). I validate the password with Phalcon\Validation\Validator\Confirmation as below:

<?php

    /**
     * @const string
     */
    const PASSWORD = 'password';

    /**
     * @const string
     */
    const CONFIRM = 'confirmPassword';

    $passw = new Password(self::PASSWORD, [
            'class' => 'form-control',
            'placeholder' => _('Password')
    ]);

    $passw2 = new Password(self::CONFIRM, [
            'class' => 'form-control',
            'placeholder' => _('Confirm password')
    ]);

    $passw2->addValidator(new Confirmation([
            "with" => [self::CONFIRM => self::PASSWORD],
            "message" => [self::CONFIRM => _('Confirmation doesn\'t match password')]
    ]));