Password Confirmation in zend framework

后端 未结 5 611
萌比男神i
萌比男神i 2021-01-03 04:32

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



        
相关标签:
5条回答
  • 2021-01-03 04:36

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

    0 讨论(0)
  • 2021-01-03 04:41

    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);
    
    0 讨论(0)
  • 2021-01-03 04:43

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

    0 讨论(0)
  • 2021-01-03 04:44

    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'));
    
    0 讨论(0)
  • 2021-01-03 04:54

    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);
    }
    
    0 讨论(0)
提交回复
热议问题