How to verify password field in zend form?

前端 未结 7 1320
旧巷少年郎
旧巷少年郎 2021-02-04 09:39

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

7条回答
  •  野的像风
    2021-02-04 10:00

    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.

提交回复
热议问题