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...
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.