Created
April 29, 2013 14:39
-
-
Save mcfdn/5481981 to your computer and use it in GitHub Desktop.
Simple password confirmation validation using Zend_Validate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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