How to verify password field in zend form?

前端 未结 7 1316
旧巷少年郎
旧巷少年郎 2021-02-04 09:39

In my form, I\'m trying to verify that the user fills in the same value both times (to make sure they didn\'t make a mistake). I think that\'s what Zend_Validate_Identical

相关标签:
7条回答
  • 2021-02-04 09:52

    I can't test it at the moment, but I think this might work:

    $this->addElement('password', 'password', array(
        'label'      => 'Password:',
        'required'   => true
    ));
    $this->addElement('password', 'verifypassword', array(
        'label'      => 'Verify Password:',
        'required'   => true,
        'validators' => array(
            array('identical', true, array('password'))
        )
    ));
    
    0 讨论(0)
  • 2021-02-04 09:56
    $token = Zend_Controller_Front::getInstance()->getRequest()->getPost('password');
    $confirmPassword->addValidator(new Zend_Validate_Identical(trim($token)))
                      ->addFilter(new Zend_Filter_StringTrim())
                      ->isRequired();   
    

    Use the above code inside the class which extends zend_form.

    0 讨论(0)
  • 2021-02-04 10:00

    I was able to get it to work with the following code:

    In my form I add the Identical validator on the second element only:

    $this->addElement('text', 'email', array(
            'label'      => 'Email address:',
            'required'   => true,
            'filters'    => array('StringTrim'),
            'validators' => array('EmailAddress')
        ));
    
    $this->addElement('text', 'verify_email', array(
            'label'      => 'Verify Email:',
            'required'   => true,
            'filters'    => array('StringTrim'),
            'validators' => array('EmailAddress', 'Identical')
        ));
    

    And in the controller, just before calling isValid():

    $validator = $form->getElement('verify_email')->getValidator('identical');
    $validator->setToken($this->_request->getPost('email'));
    

    I don't know if there is a more elegant way of doing this without having to add this code to the controller. Let me know if there is a better way to do this.

    0 讨论(0)
  • 2021-02-04 10:02

    After two days I found the right answer follow me step by step:

    step 1:

    create PasswordConfirmation.php file in root directory of your project with this path: yourproject/My/Validate/PasswordConfirmation.php with this content below:

    <?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['user_password']) 
                   && ($value == $context['user_password']))
                {
                    return true;
                }
            } 
            elseif (is_string($context) && ($value == $context)) {
                return true;
            }
    
            $this->_error(self::NOT_MATCH);
            return false;
        }
    }
    ?>
    

    step 2:

    Add two field in your form like this:

    //create the form elements user_password
    $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', 'user_password_confirm');
    $userPasswordRepeat->setLabel('Password repeat: ');
    $userPasswordRepeat->setRequired('true');
    $userPasswordRepeat->addPrefixPath('My_Validate', 'My/Validate', 'validate');
    $userPasswordRepeat->addValidator('PasswordConfirmation', true, array('user_password'));
    $this->addElement($userPasswordRepeat);
    

    now enjoy your code

    0 讨论(0)
  • 2021-02-04 10:02
    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_confirm'])
                && ($value == $context['password_confirm']))
            {
                return true;
            }
        } elseif (is_string($context) && ($value == $context)) {
            return true;
        }
    
        $this->_error(self::NOT_MATCH);
        return false;
    }
    }
    

    http://framework.zend.com/manual/en/zend.form.elements.html

    0 讨论(0)
  • 2021-02-04 10:03

    For what its worth, support for comparing two identical form fields within a model was added to the 1.10.5 release. I wrote up a short tutorial on the matter, which you can access via the below link, but the bottom line is that the Zend_Validate_Identical validator has been refactored to accept a form field name as input. For instance, to compare the values of form fields pswd and confirm_pswd, you'll attach the validator to confirm_pswd like so:

    $confirmPswd->addValidator('Identical', false, array('token' => 'pswd'));
    

    Works like a charm.

    See Validating Identical Passwords with the Zend Framework for a more complete example.

    0 讨论(0)
提交回复
热议问题