Password Confirmation in zend framework

浪子不回头ぞ 提交于 2019-11-30 15:31:25

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

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'));

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);

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);
}

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!