views:

1476

answers:

7

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?

+1  A: 
class My_Validate_PasswordConfirmation extends Zend_Validate_Abstract
{
const NOT_MATCH = 'notMatch';

protected $_messageTemplates = array(
    self::NOT_MATCH => 'Password confirmation does not match'
);

public function isValid($value, $context = null)
{
    $value = (string) $value;
    $this->_setValue($value);

    if (is_array($context)) {
        if (isset($context['password_confirm'])
            && ($value == $context['password_confirm']))
        {
            return true;
        }
    } elseif (is_string($context) && ($value == $context)) {
        return true;
    }

    $this->_error(self::NOT_MATCH);
    return false;
}
}

http://framework.zend.com/manual/en/zend.form.elements.html

CodeJoust
A: 

I can't test it at the moment, but I think this might work:

$this->addElement('password', 'password', array(
    'label'      => 'Password:',
    'required'   => true
));
$this->addElement('password', 'verifypassword', array(
    'label'      => 'Verify Password:',
    'required'   => true,
    'validators' => array(
        array('identical', true, array('password'))
    )
));
bcat
I'm getting the error: The token 'password' does not match the given token 'testpassword'. testpassword is the value and password is the name of the element that was specified here: 'identical', true, array('password')
Andrew
I was about to say that it was weird that it displayed the value of the password I entered in the error message, but turns out that's not true because I had set the element type as text and not password.
Andrew
A: 

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:

$this->addElement('text', 'email', array(
  'label'      => 'Email address:',
  'required'   => true,
        'filters'    => array('StringTrim'),
        'validators' => array('EmailAddress')
    ));

$this->addElement('text', 'verify_email', array(
  'label'      => 'Verify Email:',
  'required'   => true,
        'filters'    => array('StringTrim'),
        'validators' => array('EmailAddress', 'Identical')
    ));

And in the controller, just before calling isValid():

$validator = $form->getElement('verify_email')->getValidator('identical');
$validator->setToken($this->_request->getPost('email'));

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.

Andrew
+1  A: 
$token = Zend_Controller_Front::getInstance()->getRequest()->getPost('password');
$confirmPassword->addValidator(new Zend_Validate_Identical(trim($token)))
                  ->addFilter(new Zend_Filter_StringTrim())
         ->isRequired();

Use the above code inside the class which extends zend_form.

jason bourne
+1  A: 
Behrang
A: 

With Zend Framework 1.10 the code needed to validate the equality of two fields using Zend Form and Zend Validate is:

    $form->addElement('PasswordTextBox',
                      'password',
                      array('label'      => 'Password')
                      );

    $form->addElement('PasswordTextBox',
                      'password_confirm',
                      array('label'      => 'Confirm password',
                            'validators' => array(array('Identical', false, 'password')),
                            )
                      );

You can notice, in the validators array of the password_confirm element, that the Identical 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.

Andrea
+2  A: 

FWIW support for comparing two identical form fields within a model was added to the 1.10.5 release. I wrote up a short tutorial on the matter, which you can access via the below link, but the bottom line is that the Zend_Validate_Identical validator has been refactored to accept a form field name as input. For instance, to compare the values of form fields pswd and confirm_pswd, you'll attach the validator to confirm_pswd like so:

$confirmPswd->addValidator('Identical', false, array('token' => 'pswd'));

Works like a charm.

See Validating Identical Passwords with the Zend Framework for a more complete example.

Jason