I am a newbie to zend framework. I am developing a simple user registration application in zend. I have both add and edit functionalities in my application. I can add a new user perfectly using my application. But when I try to edit user information other than password the validators used for password and passwordConfirmation fields gives errors. I need to remove/disable validators of password and passwordConfirmation fields only when I want to edit the user informations other than password.
My form:
$password = new Zend_Form_Element_Password('password');
$password->setRequired(true)
->addFilter('StringTrim')
->addFilter('StripTags')
->addValidator('NotEmpty', false, array('messages'=>'password cannot be empty'))
->addValidator('StringLength', false, array(2, 20, 'messages'=>'password must be 2-20 character'))
->setLabel('Password:');
$this->addElement($password);
$confirmPassword = new Zend_Form_Element_Password('confirmPassword');
$confirmPassword->setRequired(true)
->addFilter('StringTrim')
->addFilter('StripTags')
->addValidator('NotEmpty', false, array('messages'=>'password do not match'))
->addValidator(new Application_Validate_PasswordConfirmation())
->setLabel('Retype password');
My validator class:
class Application_Validate_PasswordConfirmation extends Zend_Validate_Abstract
{
const NOT_MATCH = 'notMatch';
protected $_messageTemplates = array(
self::NOT_MATCH => 'Password confirmation does not match'
);
public function isValid($value, $context = null)
{
$value = (string) $value;
$this->_setValue($value);
if (is_array($context)) {
if (isset($context['password']) && ($value == $context['password']))
{
return true;
}
} elseif (is_string($context) && ($value == $context)) {
return true;
}
$this->_error(self::NOT_MATCH);
return false;
}
}