In my form, I'm trying to verify that the user fills in the same value both times (to make sure they didn't make a mistake). I think that's what Zend_Validate_Identical
is for, but I'm not quite sure how to use it. Here's what I've got so far:
$this->addElement('password', 'password', array(
'label' => 'Password:',
'required' => true,
'validators' => array(
'Identical' => array(What do I put here?)
)
));
$this->addElement('password', 'verifypassword', array(
'label' => 'Verify Password:',
'required' => true,
'validators' => array(
'Identical' => array(What do I put here?)
)
));
Do I need it on both elements? What do I put in the array?
I was able to get it to work with the following code:
In my form I add the Identical validator on the second element only:
And in the controller, just before calling
isValid()
:I don't know if there is a more elegant way of doing this without having to add this code to the controller. Let me know if there is a better way to do this.
Use the above code inside the class which extends zend_form.
With Zend Framework 1.10 the code needed to validate the equality of two fields using Zend Form and Zend Validate is:
You can notice, in the validators array of the
password_confirm
element, that theIdentical
validator is passed as array, the semantics of that array is: i) Validator name, ii) break chain on failure, iii) validator options As you can see, it's possible to pass the field name instead of retrieving the value.After two days I found the right answer follow me step by step:
step 1:
create
PasswordConfirmation.php
file in root directory of your project with this path:yourproject/My/Validate/PasswordConfirmation.php
with this content below:step 2:
Add two field in your form like this:
now enjoy your code
I can't test it at the moment, but I think this might work:
http://framework.zend.com/manual/en/zend.form.elements.html