Skip to content

Instantly share code, notes, and snippets.

@mcfdn
Created April 29, 2013 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcfdn/5481981 to your computer and use it in GitHub Desktop.
Save mcfdn/5481981 to your computer and use it in GitHub Desktop.
Simple password confirmation validation using Zend_Validate
<?php
class App_Validate_PasswordsMatch extends Zend_Validate_Abstract
{
const PASSWORD_MISMATCH = 'passwordMismatch';
/**
* The field name of the password
*
* @var string
*/
protected $PasswordField;
/**
* The field name of the password confirmation
*
* @var string
*/
protected $ConfirmField;
/**
* @var array
*/
protected $_messageTemplates = array(
self::PASSWORD_MISMATCH => 'Password fields did not match'
);
/**
* Set the field names
*
* @param string $passwordField
* @param string $confirmField
*/
public function __construct($passwordField = 'Password', $confirmField = 'ConfirmPassword', $message = null)
{
if(null !== $message) {
$this->_messageTemplates[self::PASSWORD_MISMATCH] = $message;
}
$this->PasswordField = $passwordField;
$this->ConfirmField = $confirmField;
}
/**
* Check the two password field match
*
* @param mixed $value
* @param array $formContext [optional] The submitted form data
* @return boolean
* @throws Exception
*/
public function isValid($value, $formContext = null)
{
if(!isset($formContext[$this->PasswordField]) ||
!isset($formContext[$this->ConfirmField])) {
throw new Exception($this->PasswordField . ' or ' . $this->ConfirmField .
' not found in ' . __METHOD__);
}
if($formContext[$this->PasswordField] !== $formContext[$this->ConfirmField]) {
$this->_error(self::PASSWORD_MISMATCH);
return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment