Validation Zend_Form:相同字段在实际相同时返回false

Validation Zend_Form:相同字段在实际相同时返回false,validation,zend-framework,zend-form,password-confirmation,Validation,Zend Framework,Zend Form,Password Confirmation,我已经为此挣扎了一个小时,但仍然不明白到底出了什么问题。 下面是我设置两个字段的方法: $password = $this->createElement('password', 'password'); $password->setLabel('Password'); $password->setRequired('true'); $password->addValidator(new Zend_Validate_Regex('/^([a-zA-Z0-9]*[0-9]+[

我已经为此挣扎了一个小时,但仍然不明白到底出了什么问题。
下面是我设置两个字段的方法:

$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);
我尝试了两个验证器(+注释的那个),但都不起作用。 这些声明来自和

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

猜猜看?谢谢大家!

编辑: 感谢XDebug,我注意到当调用
Zend\u Validate\u identity
isValid()
函数时,我的
$token
等于'password',而我的
$value
等于'P4ssword'(我在确认密码字段中输入的密码)。发生了什么

EDIT2
尝试了这两种解决方案:但它们都不适用于我。使用通用IdenticalField验证器,它找不到我的“密码”字段和第二个验证器只是返回两个字段不匹配的结果,对于XDebug,我再次发现它仍在寻找匹配我的密码“P4ssword”和我作为字段名给出的单词“password”的方法…

是否可能您没有使用当前的Zend Framework版本?类中的代码看起来应该与您之前的代码一样有效pect it(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'))从我的观点来看,问题是你将验证器设置为password元素,而不是confirm\u password。密码值在$value中,confirm在$context中。你可以移动验证器或将令牌更改为confirm\u password。我真的不明白你在说什么。你的意思是我应该将验证器添加到“password”中吗元素,或者你是暗示我错了吗?因为我的代码就是我问题中的样子。顺便说一句,我以前也试过把它放在password元素上,但没有更好的效果。奇怪的东西。