0

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 Answer 1

0

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

is there a way I could for sure tell what version I'm using ?
$context contains: ('controller' => 'user', 'action' => 'dovalidation', 'module' => 'default', 'confirm_password' => 'P4ssword')
Than from my point of view the problem is you set the validator to password element, instead of confirm_password. The password value is in $value and confirm is in $context. You can either move the validator or change the token to confirm_password
I didn't really understand what you were saying. Did you mean I should add the validator to 'password' element or did you imply that I do it by mistake? Because my code is exactly how it looks in my question. Btw, I've also tried to put it on password element before but doesn't work any better. Weird stuff.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.