2

I did register form in with zend form

$password = new Zend_Form_Element_Password('password'); $password->setLabel($this->_translate->_("Password:")) ->setRequired(true) ->addValidator('stringLength', true, array(4, 32)); $confirmPassword = new Zend_Form_Element_Password('confirmpassword'); $confirmPassword->setLabel($this->_translate->_("Confirm Password:")) ->setRequired(true); 

I control password and confirmpassword in controller. if password and confirmpassword don't match then add error message under confirmpassword textbox. how i do?

4 Answers 4

5

Override isValid in your form

/** * Validate the form, check passwords. * * @param array $data * @return boolean */ public function isValid($data) { $valid = parent::isValid($data); if ($this->getValue('password') !== $this->getValue('password2')) { $valid = false; $this->password2->addError('Passwords don\'t match.'); } return $valid; } 
Sign up to request clarification or add additional context in comments.

Comments

2

The concept basically boils down to adding a Zend_Validate_Identical validator to the 'confirmpassword' field, using the data from the $this->_request->getParam('password') to test against. I use a custom method on an extended Zend_Form to process the post data from all my forms, it isn't an exact solution for you, but perhaps my code from my "EditUser" form can point you in the right direction.

From the controller:

// $form is a MW_Form_EditUser. if ($this->_request->isPost() && $form->process($this->_request->getPost())) { // successful form - redirect or whatever here } 

From the MW_Form_EditUser class:

public function process(array $data) { // gets a copy of the user we are editing $user = $this->getEditable(); // checks to see if the user we are editing is ourself $isSelf = ($user->id == MW_Auth::getInstance()->getUser()->id); // if the new_pass field is non-empty, add validators for confirmation of password if (!empty($data['new_pass'])) { $this->new_pass2->setAllowEmpty(false)->addValidator( new Zend_Validate_Identical($data['new_pass']) ); if ($curpass = $this->current_password) $curpass->setAllowEmpty(false); } if ($this->delete && !empty($data["delete"])) { $this->delete->setValue(true); $user->delete(); return true; } if ($this->isValid($data)) { /// saves the data to the user $user->email = $this->email->getValue(); $user->name = $this->name->getValue(); if ($password = $this->new_pass->getValue()) $user->password = $password; if (!$isSelf) { if ($this->super->getValue()) { $user->setGroups(array(MW_Auth_Group_Super::getInstance())); } else { $user->setGroups(array(MW_Auth_Group_User::getInstance())); } } $user->save(); return true; } return false; } 

1 Comment

I think, it's better to not be confusing: to name function isValid(), create in it Zend_Validate_Identical and just return parent::isValid(). This solution is more natural and needs less code
1
//inside form public function isValidPSW($data) { $valid = parent::isValid($data); if ($data['pswd'] !== $data['pswd2']) { $valid = false; $this->pswd->addError('Passwords don\'t match.'); } return $valid; } 

Comments

-1

Using Zend_Validate_Identical is a good solution for you by Zend. I will give you another choice. jQuery. When you are using Zend_Validate_Identical form will go to the server and server will validate it. If passwords are not same it will return an error message. If you use jQuery form will not go to the server unless passwords are same.

1 Comment

You have to have server side validation while client side validation is nice to have. Client side without server side is useless.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.