Zend_Form: identical fields returning false when actually identical

238 views Asked by At

I've been struggling with this for an hour now and still can't understand what's wrong.
Here's how I set my two fields:

$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);

I've tried both validators (+ the one commented) but none works. These declarations come from www.wjgilmore.com/blog/entry/validating_identical_passwords_with_the_zend_framework and https://stackoverflow.com/a/3653416/1300454

Both declarations always return "The two given tokens do not match" eventho I'm sure these two fields contain the exact same string.

Any guess ? Thank you!

EDIT: just noticed thanks to XDebug that when Zend_Validate_Identical's isValid() function is called, my $token equals 'password' while my $value equals 'P4ssword' (the password I entered in my confirm_password field. What's going on?

EDIT2: tried with these two solutions: emanaton.com/code/php/validateidenticalfield and stackoverflow.com/a/1628611/1300454 but none of them work for me either. With the generic IdenticalField validator it can't find my "password" field and the second validator just returns that the two field don't match and with XDebug again I found out that it's still looking to match my password 'P4ssword' to the word 'password' that I gave as the field name...

1

There are 1 answers

4
Tomáš Fejfar On

Is it possible you're not using current Zend Framework version? The code in the class looks like it should work as you expect it (1.11). Are you using $form->isValid() or ->isValidPartial()? Try editing the code of ZF and dumping the contents of $context variable. It looks like your code is not filling the $context variable as it should.

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;
}