Monday, April 11, 2011

Password confirmation using Zend Form and Validators | Thoughts, Codes and Games

Password confirmation using Zend Form and Validators | Thoughts, Codes and Games

A while back i needed a password confirmation field for my users. I looked and found this neat little snippet. I cant remember who created it originally so i cant give credits to the person but its quite easy to use. Just add this to your library and create a new validator object like this.
class App_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_confirm']) &&
($value == $context['password_confirm'])) {
return true;
}
} elseif (is_string($context) && ($value == $context)) {
return true;
}
$this->_error(self::NOT_MATCH);
return false;
}
}
This would be in your form where you want to have password confirmation.
$passwordConfirmation = new App_Validate_PasswordConfirmation();
$password = $this->addElement('password', 'password', array(
'filters' => array('StringTrim'),
'validators' => array(
$passwordConfirmation,
array('Alnum'),
array('StringLength', false, array(6, 100)),
),
'class' => 'input-text',
'required' => true,
'label' => 'Password',
));
$password_confirm = $this->addElement('password', 'password_confirm', array(
'filters' => array('StringTrim'),
'validators' => array(
$passwordConfirmation,
array('Alnum'),
array('StringLength', false, array(6, 100)),
),
'class' => 'input-text',
'required' => true,
'label' => 'Confirm Password',
));
view raw snippet.php This Gist brought to you by GitHub.

0 评论:

Post a Comment