Zend_Form: how to check 2 fields are the same

I created a form to add the user to the database and make the user available for login.

Now I have two password fields (the second is for checking the first). How can I add a validator for this kind of validation in zend_form?

This is my code for two password fields:

$password = new Zend_Form_Element_Password('password', array( 'validators'=> array( 'Alnum', array('StringLength', array(6,20)) ), 'filters' => array('StringTrim'), 'label' => 'Wachtwoord:' )); $password->addFilter(new Ivo_Filters_Sha1Filter()); $password2 = new Zend_Form_Element_Password('password', array( 'validators'=> array( 'Alnum', array('StringLength', array(6,20)) ), 'filters' => array('StringTrim'), 'required' => true, 'label' => 'Wachtwoord:' )); $password2->addFilter(new Ivo_Filters_Sha1Filter()); 
+7
php validation zend-framework zend-form
source share
4 answers

When I searched the same, I found this very well-working generic validator for identical fields. I did not find it now, so just send the code ...

 <?php class Zend_Validate_IdenticalField extends Zend_Validate_Abstract { const NOT_MATCH = 'notMatch'; const MISSING_FIELD_NAME = 'missingFieldName'; const INVALID_FIELD_NAME = 'invalidFieldName'; /** * @var array */ protected $_messageTemplates = array( self::MISSING_FIELD_NAME => 'DEVELOPMENT ERROR: Field name to match against was not provided.', self::INVALID_FIELD_NAME => 'DEVELOPMENT ERROR: The field "%fieldName%" was not provided to match against.', self::NOT_MATCH => 'Does not match %fieldTitle%.' ); /** * @var array */ protected $_messageVariables = array( 'fieldName' => '_fieldName', 'fieldTitle' => '_fieldTitle' ); /** * Name of the field as it appear in the $context array. * * @var string */ protected $_fieldName; /** * Title of the field to display in an error message. * * If evaluates to false then will be set to $this->_fieldName. * * @var string */ protected $_fieldTitle; /** * Sets validator options * * @param string $fieldName * @param string $fieldTitle * @return void */ public function __construct($fieldName, $fieldTitle = null) { $this->setFieldName($fieldName); $this->setFieldTitle($fieldTitle); } /** * Returns the field name. * * @return string */ public function getFieldName() { return $this->_fieldName; } /** * Sets the field name. * * @param string $fieldName * @return Zend_Validate_IdenticalField Provides a fluent interface */ public function setFieldName($fieldName) { $this->_fieldName = $fieldName; return $this; } /** * Returns the field title. * * @return integer */ public function getFieldTitle() { return $this->_fieldTitle; } /** * Sets the field title. * * @param string:null $fieldTitle * @return Zend_Validate_IdenticalField Provides a fluent interface */ public function setFieldTitle($fieldTitle = null) { $this->_fieldTitle = $fieldTitle ? $fieldTitle : $this->_fieldName; return $this; } /** * Defined by Zend_Validate_Interface * * Returns true if and only if a field name has been set, the field name is available in the * context, and the value of that field name matches the provided value. * * @param string $value * * @return boolean */ public function isValid($value, $context = null) { $this->_setValue($value); $field = $this->getFieldName(); if (empty($field)) { $this->_error(self::MISSING_FIELD_NAME); return false; } elseif (!isset($context[$field])) { $this->_error(self::INVALID_FIELD_NAME); return false; } elseif (is_array($context)) { if ($value == $context[$field]) { return true; } } elseif (is_string($context) && ($value == $context)) { return true; } $this->_error(self::NOT_MATCH); return false; } } ?> 
+3
source share

The current version of Zend_Validate has a built-in embedded system - while there are many other answers, it seems that everyone needs to pass the value of Zend_Validate_Identical . Although this may be required at some point, you can pass the name of another element.

In the Zend_Validate section of the reference guide :

Zend_Validate_Identical also supports comparing form elements. This can be done using the element name as a token. See the following example:

 $form->addElement('password', 'elementOne'); $form->addElement('password', 'elementTwo', array( 'validators' => array( array('identical', false, array('token' => 'elementOne')) ) )); 

Using the element name from the first element as a token for the second element, the validator checks if the second element is equal to the first element. If your user does not enter two identical values, you will receive a validation error.

+40
source share

this is how i did it :)

create the input of the first input, then enter the second input of the second input and add an identification validator with the data from the previous password entry.

 $password_2->addValidator('identical', false, $this->_request->getPost('password')); 
+1
source share

You can access all form fields from the validator, and you can also use the constructor to pass additional arguments

 class Example_Validator extends Zend_Validate_Abstract{ const NOT_IDENTICALL = 'not same'; private $testValue; public function __construct( $arg ) { $this->testValue = $arg; } protected $_messageTemplates = array( self::NOT_IDENTICALL => "Passwords aren't same" ); public function isValid( $value, $context = null ) { echo $context['password']; echo '<br>'; echo $this->testValue; return true; } } 

to call this validator

 $form = new Zend_Form(); $form->setAction('success'); $form->setMethod('post'); $form->addElement('text', 'username'); $usernameElement = $form->getElement('username'); $form->addElement('password', 'password'); $passwordElement = $form->getElement('password'); $myValidator2 = new Example_Validator("Hello !"); $passwordElement->addValidator($myValidator2, true); $form->addElement('submit', 'submit'); $submitButton = $form->getElement('submit'); 
0
source share

All Articles