Password confirmation using Zend Form and Validators

A while back i needed a password confirmation field for my users. I looked and found this neat little snippet. I cant remember who created it originally so i cant give credits to the person but its quite easy to use. Just add this to your library and create a new validator object like this.

This would be in your form where you want to have password confirmation.

9 thoughts on “Password confirmation using Zend Form and Validators

  1. Pingback: Zend Framework in Action » Christian Kirkegaard: Password confirmation using Zend Form and Validators

  2. David Weinraub

    Nice!

    It would be more general purpose if you could configure the validator with the names of the two password fields. Maybe passed in the constructor?

    Thanks for a great snippet. ;-)

  3. Pingback: Zend Framework News » Blog Archive » Passwortvalidierung mit Zend_Form und Zend_Validator

  4. Pingback: Linkpool Nummer 5 | PHP Gangsta - Der PHP Blog

  5. roti

    Im getting the Not match always, even if I put same passwords in both of the textbox..

    MyValidatePasswordConfirmation

    const NOT_MATCH = ‘notMatch’;

    protected $_messageTemplates = array(self::NOT_MATCH => 'Passwords do

    not match.’);

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

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

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

    And my Form
    $password = new ZendFormElement_Password(‘passwd’,array(
    ‘required’ => true,
    ‘label’ => ‘Password’,
    ‘size’ => 11
    ));

    $confirmation = new Zend_Form_Element_Password('confirmation',array(
    'required' => true,
    'label' => 'Re-Password',
    'size' => 11
    ));
    //$confirmation->addValidator('PasswordConfirmation',array('passwd'), true);

    Could you help me please…

  6. Christian Kirkegaard Post author

    the validator doesnt take any arguments. Just parse the validator to your elements

    
    // more form code here...
    // $password = new ZendFormElementPassword(.....);
    // $confirm = new Zend
    FormElementPassword(.....);

    $confirmValidator = new MyValidatePasswordConfirmation();
    $password->addValidator($confirmValidator);
    $confirm->addValidator($confirmValidator);

  7. PHP Consultant

    Nice way, but there is an already existing validator called ZendValidateIdentical

    ZendValidateIdentical supports also the comparison of form elements. This can be done by using the element’s name as token. See the following example:

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

Leave a Reply

Your email address will not be published. Required fields are marked *