I add this class to library/My/Validate/PasswordConfirmation.php
I think you may want $context['user_password']
as that is the name of your "first" password element
A less elegant and simpler way to do it:
$password = new Zend_Form_Element_Password('password');
$password->setLabel('Password:')
->addValidator('StringLength', false, array(6,24))
->setLabel('Choose your password:')
->setRequired(true);
$password2 = new Zend_Form_Element_Password('password-confirm');
$password2->setLabel('Confirm:')
->addValidator('StringLength', false, array(6,24))
->setLabel('Confirm your password:')
->addValidator(new Zend_Validate_Identical($_POST['password']))
->setRequired(true);
Make the validator reusable. Do not hard code field name in the validator. Look at this IdenticalField Validator which is more universal.
You don't need to override the Zend_Form->isValid method or use the superglobal $_POST, check this:
$frmPassword1=new Zend_Form_Element_Password('password');
$frmPassword1->setLabel('Password')
->setRequired('true')
->addFilter(new Zend_Filter_StringTrim())
->addValidator(new Zend_Validate_NotEmpty());
$frmPassword2=new Zend_Form_Element_Password('confirm_password');
$frmPassword2->setLabel('Confirm password')
->setRequired('true')
->addFilter(new Zend_Filter_StringTrim())
->addValidator(new Zend_Validate_Identical('password'));
There is a bettter way to do that. In your form put the identical validator on the confirmation passoword field, and then just overwrite $form->isValid() method to set the value to be validated:
public function __construct($options = NULL)
{
// ...
$confirm->addValidator('Identical');
// ...
}
public function isValid($data)
{
$confirm = $this->getElement('confirm_password');
$confirm->getValidator('Identical')->setToken($data['password']);
return parent::isValid($data);
}