Zend 框架中的密码确认 [英] Password Confirmation in zend framework

查看:22
本文介绍了Zend 框架中的密码确认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将这个类添加到 library/My/Validate/PasswordConfirmation.php

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

推荐答案

我想你可能想要 $context['user_password'] 因为那是你的第一个"密码元素的名称

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

这篇关于Zend 框架中的密码确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆