How to verify password field in zend form?

前端 未结 7 1329
旧巷少年郎
旧巷少年郎 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 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:

     '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

提交回复
热议问题