Password Confirmation in zend framework

2019-02-10 19:02发布

I add this class to library/My/Validate/PasswordConfirmation.php

<?php 
require_once 'Zend/Validate/Abstract.php';
class My_Validate_PasswordConfirmation extends Zend_Validate_Abstract
{
    const NOT_MATCH = 'notMatch';

    protected $_messageTemplates = array(
        self::NOT_MATCH => 'Password confirmation does not match'
    );

    public function isValid($value, $context = null)
    {
        $value = (string) $value;
        $this->_setValue($value);

        if (is_array($context)) {
            if (isset($context['password'])
                && ($value == $context['password']))
            {
                return true;
            }
        } elseif (is_string($context) && ($value == $context)) {
            return true;
        }

        $this->_error(self::NOT_MATCH);
        return false;
    }
}
?>

then I create two field in my form like this :

       $userPassword = $this->createElement('password', 'user_password');
    $userPassword->setLabel('Password: ');
    $userPassword->setRequired('true');
    $this->addElement($userPassword);

    //create the form elements user_password repeat
    $userPasswordRepeat = $this->createElement('password', 'password_confirm');
    $userPasswordRepeat->setLabel('Password repeat: ');
    $userPasswordRepeat->setRequired('true');
    $userPasswordRepeat->addPrefixPath('My_Validate','My/Validate','validate');

    $userPasswordRepeat->addValidator('PasswordConfirmation');
    $this->addElement($userPasswordRepeat)

everything is good but when i submit form always I get the 'Password confirmation does not match' message ? What's Wrong in my code

5条回答
来,给爷笑一个
2楼-- · 2019-02-10 19:15

Make the validator reusable. Do not hard code field name in the validator. Look at this IdenticalField Validator which is more universal.

查看更多
你好瞎i
3楼-- · 2019-02-10 19:16

You don't need to override the Zend_Form->isValid method or use the superglobal $_POST, check this:

$frmPassword1=new Zend_Form_Element_Password('password');
$frmPassword1->setLabel('Password')
    ->setRequired('true')
    ->addFilter(new Zend_Filter_StringTrim())
    ->addValidator(new Zend_Validate_NotEmpty());

$frmPassword2=new Zend_Form_Element_Password('confirm_password');
$frmPassword2->setLabel('Confirm password')
    ->setRequired('true')
    ->addFilter(new Zend_Filter_StringTrim())
    ->addValidator(new Zend_Validate_Identical('password'));
查看更多
成全新的幸福
4楼-- · 2019-02-10 19:37

There is a bettter way to do that. In your form put the identical validator on the confirmation passoword field, and then just overwrite $form->isValid() method to set the value to be validated:

public function __construct($options = NULL)
{
   // ...
   $confirm->addValidator('Identical');
   // ...
}
public function isValid($data)
{
    $confirm = $this->getElement('confirm_password');
    $confirm->getValidator('Identical')->setToken($data['password']);
    return parent::isValid($data);
}
查看更多
老娘就宠你
5楼-- · 2019-02-10 19:39

A less elegant and simpler way to do it:

    $password = new Zend_Form_Element_Password('password');
    $password->setLabel('Password:')
            ->addValidator('StringLength', false, array(6,24))
            ->setLabel('Choose your password:')
            ->setRequired(true);

    $password2 = new Zend_Form_Element_Password('password-confirm');
    $password2->setLabel('Confirm:')
            ->addValidator('StringLength', false, array(6,24))
            ->setLabel('Confirm your password:')
            ->addValidator(new Zend_Validate_Identical($_POST['password']))
            ->setRequired(true);
查看更多
小情绪 Triste *
6楼-- · 2019-02-10 19:41

I think you may want $context['user_password'] as that is the name of your "first" password element

查看更多
登录 后发表回答