While working on a Zend Framework project, I’ve found myself in need of a “same as” validator. If you’re familiar with Zend Framework’s form system, then you know what a validator is, if not, enhance your knowledge on the topic by reading this page from ZF’s manual.
My validator was supposed to check if the same value was entered into two password fields, to avoid the user’s typos in passwords. You know, those annoying “please re-type your password” fields. At the time, I couldn’t find a good validator for this purpose, so I’ve decided to write my own. The source code is available here:
/**
* Checks if a field has the same value as another,
* very useful in validating passwords
*
* @package Generic Validate
* @author Tudor Barbu <miau@motane.lu>
* @copyright MIT
*/
class Generic_Validate_SameAs extends Zend_Validate_Abstract {
/**
* Validation failure message key for when the values are not
* the same
*/
const NOT_THE_SAME = 'not_the_same';
/**
* the external element that we check the value against
*
* @var Zend_Form_Element
*/
protected $_element;
/**
* Validation failure message template definitions
*
* @var array
*/
protected $_messageTemplates = array(
self::NOT_THE_SAME => 'The two values are not identical',
);
/**
* Can receive a Zend_Form_Element parameter that will be used
* into the validation process
*
* @param Zend_Form_Element
*/
public function __construct(Zend_Form_Element $element = null) {
if(null !== $element) {
$this->setElement($element);
}
}
/**
* Set the element
*
* @param Zend_Form_Element $element
* @return void
*/
public function setElement(Zend_Form_Element $element) {
$this->_element = $element;
}
/**
* gets the element
*
* @return Zend_Form_Element
*/
public function getElement() {
return $this->_element;
}
/**
* overrides isValid from Zend_Validate_Interface
*
* @param string $value
* @return bool
*/
public function isValid($value) {
if(null === $this->_element) {
require_once 'Zend/Exception.php';
throw new Zend_Exception('You must add a Zend_Form_Element to the SameAs validator prior to calling the isValid() method');
}
if($value != $this->_element->getValue()) {
$this->_error(self::NOT_THE_SAME);
return false;
}
return true;
}
}
// EOF
PS: The formatting plugin I’m using might not display everything correctly
Aside from that, It’s quite easy to use:
// create some filters
$stringTrimFilter = new Zend_Filter_StringTrim();
$stripTagsFilter = new Zend_Filter_StripTags();
// other validators
$notEmptyValidator = new Zend_Validate_NotEmpty();
$notEmptyValidator->setMessage('This field is required');
// create the SameAs validator
$sameAsValidator = new Generic_Validate_SameAs();
$sameAsValidator->setMessage('The 2 fields do not coincide', Generic_Validate_SameAs::NOT_THE_SAME);
// create the main password field
$password = new Zend_Form_Element_Password('password');
$password->setOptions(
array(
'label' => 'Password',
'required' => true,
'filters' => array($stringTrimFilter, $stripTagsFilter),
'validators' => array($notEmptyValidator),
)
);
// add this field to the SameAs validator
$sameAsValidator->setElement($password);
// create the new
$validateNewPassword = new Zend_Form_Element_Password('validateNewPassword');
$validateNewPassword->setOptions(
array(
'label' => 'Re-type the password',
'required' => true,
'filters' => array($stringTrimFilter, $stripTagsFilter),
'validators' => array($notEmptyValidator, $sameAsValidator), // add it as a validator to the second field
)
);
If the value entered in the second field isn’t the same as the value entered in the first field, the validator will signal an error. Simple as that.
I’ve downloaded the 1.8 version of Zend Framework and looked over Zend_Tool these last days. What can I say. What is there to say. Zend_Tool is cvasi-useless. You can use it to create a new project, as a shortcut to
I’m going to hold a presentation about Zend Framework at PHP Geek Meet in Cluj, Romania. Everyone’s invited.