Confirm password with customized error message using zend_form

Below was my first version of password and confirm password form inputs.

$password = $this->createElement('password','password');
$password->setLabel('Password: *')
->addValidator('StringLength', false, array(6, 60))
->setRequired(true);
$confirmPassword->setLabel('Confirm Password: *')
->addValidator("identical" , true, array('password' ))
->setRequired(true);

The confirmation of passwords validation working fine with its default error message of "* The two given tokens do not match". But I want to replace with my own customized message like "Your passwords doesn't match.".

I first came to page http://framework.zend.com/manual/en/zend.form.elements.html#zend.form.el... . While for function addValidator, it says:

Note: Providing Custom Validator Error Messages
Some developers may wish to provide custom error messages for a validator. The $options argument of the Zend_Form_Element::addValidator() method allows you to do so by providing the key 'messages' and mapping it to an array of key/value pairs for setting the message templates. You will need to know the error codes of the various validation error types for the particular validator.

But it does not hint what the "error codes" are and where to find them! I think the zf document regarding this really need to be improved.

After 30mins with Google, I found a solution below by declaring and customizing the validation object before using it:

$identical = new Zend_Validate_Identical();
$identical ->setMessage("Your passwords do not match.");
$identical ->setToken('password');
$confirmPassword = $this->createElement('password','confirmPassword');
$confirmPassword->setLabel('Confirm Password: *')
->addValidator($identical )
->setRequired(true);

I think it still looks intuitive.

English

Comments

While later I found another way to do this with the help of function "addErrorMessage":

$confirmPassword->setLabel('Confirm Password: *')
->addValidator("identical",true,array("token"=>"password"))
->addErrorMessage('The passwords do not match')
->setRequired(true);

Add new comment