【问题标题】:Zend_Form: identical fields returning false when actually identicalZend_Form:相同的字段在实际相同时返回 false
【发布时间】:2012-05-18 08:22:32
【问题描述】:

我已经为此苦苦挣扎了一个小时,但仍然无法理解问题所在。
以下是我设置两个字段的方式:

$password = $this->createElement('password', 'password');
$password->setLabel('Password');
$password->setRequired('true');
$password->addValidator(new Zend_Validate_Regex('/^([a-zA-Z0-9]*[0-9]+[a-z]*[A-Z]+[a-zA-Z0-9]*)|([a-zA-Z0-9]*[A-Z]+[a-z]*[0-9]+[a-zA-Z0-9]*)$/'));
$password->setAttrib('size', 25);
$password->setAttrib('length', 150);
$this->addElement($password);

$confirm_password = $this->createElement('password', 'confirm_password');
$confirm_password->setLabel('Confirm');
$confirm_password->setRequired('true');
$confirm_password->setAttrib('size', 25);
$confirm_password->setAttrib('length', 150);

$confirm_password->addValidator('Identical', false, array('token' => 'password'));
//$confirm_password->addValidator(new Zend_Validate_Identical(array('token' => 'password')));

$this->addElement($confirm_password);

我已经尝试了两个验证器(+ 评论的那个),但没有一个有效。 这些声明来自www.wjgilmore.com/blog/entry/validating_identical_passwords_with_the_zend_framework 和https://stackoverflow.com/a/3653416/1300454

两个声明总是返回“两个给定的标记不匹配”,即使我确定这两个字段包含完全相同的字符串。

有什么猜测吗?谢谢!

编辑: 感谢XDebug,刚刚注意到当Zend_Validate_Identical的isValid()函数被调用时,我的$token等于'密码',而我的$value等于'P4ssword'(我在confirm_password字段中输入的密码。这是怎么回事?

EDIT2: 尝试了这两种解决方案:emanaton.com/code/php/validateidenticalfield 和stackoverflow.com/a/1628611/1300454,但它们都不适合我。使用通用 IdenticalField 验证器,它找不到我的“密码”字段,第二个验证器只返回两个字段不匹配,再次使用 XDebug,我发现它仍在寻找将我的密码“P4ssword”与单词匹配我作为字段名称提供的“密码”...

【问题讨论】:

    标签: validation zend-framework zend-form password-confirmation


    【解决方案1】:

    您可能没有使用当前的 Zend Framework 版本吗?类中的代码看起来应该像您期望的那样工作(1.11)。你使用的是 $form->isValid() 还是 ->isValidPartial()?尝试编辑 ZF 的代码并转储 $context 变量的内容。看起来您的代码没有按应有的方式填充 $context 变量。

    public function isValid($value, $context = null)
    {
        $this->_setValue((string) $value);
    
        if (($context !== null) && isset($context) && array_key_exists($this->getToken(), $context)) {
            $token = $context[$this->getToken()];
        } else {
            $token = $this->getToken();
        }
    
        if ($token === null) {
            $this->_error(self::MISSING_TOKEN);
            return false;
        }
    
        $strict = $this->getStrict();
        if (($strict && ($value !== $token)) || (!$strict && ($value != $token))) {
            $this->_error(self::NOT_SAME);
            return false;
        }
    
        return true;
    }
    

    【讨论】:

    • 有没有办法确定我使用的是什么版本?
    • $context 包含:('controller' => 'user', 'action' => 'dovalidation', 'module' => 'default', 'confirm_password' => 'P4ssword')
    • 从我的角度来看,问题在于您将验证器设置为密码元素,而不是确认密码。密码值在 $value 中,confirm 在 $context 中。您可以移动验证器或将令牌更改为 confirm_password
    • 我不太明白你在说什么。您的意思是我应该将验证器添加到“密码”元素还是暗示我做错了?因为我的代码正是它在我的问题中的样子。顺便说一句,我之前也尝试过将其放在密码元素上,但效果并不好。奇怪的东西。
    猜你喜欢
    • 2016-12-18
    • 1970-01-01
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    • 2021-04-23
    • 1970-01-01
    相关资源
    最近更新 更多