When creating user registration and password-related models you’ll likely need an easy way to compare the user’s chosen and confirmed passwords. Yet despite the Zend Framework having long supported over two dozen input validation features, it wasn’t particularly easy to compare input associated with two different form fields without breaking tier separation best practices. That all changed with the Zend Framework 1.10.5 release, which finally added support for this useful feature.
Support was added by refactoring the Zend_Validate_Identical validator. By passing an associative array into the method containing a key named token which is assigned the name of the form field you’d like to compare, you can ensure that the user input associated with these two fields is identical. Here’s an example:
$pswd = new Zend_Form_Element_Password('pswd'); $pswd->setLabel('New Password:'); $pswd->setAttrib('size', 35); $pswd->setRequired(true); $pswd->removeDecorator('label'); $pswd->removeDecorator('htmlTag'); $pswd->removeDecorator('Errors'); $pswd->addValidator('StringLength', false, array(4,15)); $pswd->addErrorMessage('Please choose a password between 4-15 characters'); $confirmPswd = new Zend_Form_Element_Password('confirm_pswd'); $confirmPswd->setLabel('Confirm New Password:'); $confirmPswd->setAttrib('size', 35); $confirmPswd->setRequired(true); $confirmPswd->addValidator('Identical', false, array('token' => 'pswd')); $confirmPswd->addErrorMessage('The passwords do not match');