views:

1693

answers:

5

Hi 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?

+2  A: 

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;
}
gnarf
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
valya
A: 

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.

Erkan BALABAN
You have to have server side validation while client side validation is nice to have. Client side without server side is useless.
smack0007
+3  A: 

A clean solution would be to create a custom Validator class

From the Zend Framework Reference Guide:

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'])
                && ($value == $context['password']))
            {
                return true;
            }
        } elseif (is_string($context) && ($value == $context)) {
            return true;
        }

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

The $context variable allows the validator to access other form fields to perform validation.

You'd then type:

$confirmPassword->addValidator('My_Validate_PasswordConfirmation');

This method ensures better separation of concerns - it's probably not something that should be dealt with in your controller.

Hope that helps.

Richard Nguyen
+1  A: 

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;
    }
auto
A: 

@Auto

Thanks For your Solution

abser