user1559230
user1559230

Reputation: 2821

Identical field checking is not working properly for zend form

I have created a zend form where I have a password and confirm password filed. I am using same form for add and update/edit the database. My code is working fine when I want to add a new password but don't work when I want to edit already existing one.

My form:

    $password = new Zend_Form_Element_Password('password');
    $password->setRequired(true)
    ->addFilter('StringTrim')
    ->addFilter('StripTags')
    ->addValidator('NotEmpty', false, array('messages'=>'password cannot be empty'))
    ->addValidator('StringLength', false, array(5, 25, 'messages'=>'password must be 5-30 character'))
    ->setLabel('Password:');
    $this->addElement($password);

    $confirmPassword = new Zend_Form_Element_Password('confirmPassword');
    $confirmPassword->setRequired(true)
    ->addFilter('StringTrim')
    ->addFilter('StripTags')
    ->addValidator('NotEmpty', false, array('messages'=>'password don\'t match'))
    ->addValidator(new School_Validate_PasswordConfirmation())
    ->setLabel('Confirm Password');
    $this->addElement($confirmPassword);

my School_Validate_PasswordConfirmation class:

class School_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;
     }
}

When I want to edit other fields other than password the it gives the below error messages. But this messages will only be shown when I enter the user for the first time.

Errors:

password cannot be empty
password don't match

Thanks in advance.

Upvotes: 0

Views: 146

Answers (1)

drew010
drew010

Reputation: 69967

When the form is in edit mode, just remove the validators from the element.

public function someAction()
{
    $form = new Application_Form_YourForm();

    if ('in edit mode') {
        $form->getElement('password')->clearValidators();
        $form->getElement('confirmPassword')->clearValidators();
    }

    if ($this->getRequest()->isPost()) {
        $valid = $form->isValid($this->getRequest()->getPost());

        if ($valid) {
            // ...
        } else {
            // ...
        }
    }
}

If you allow someone to change their password by filling in those fields, add an additional check to see if anything is entered into the password field when in edit mode. If they have filled in the password, leave the validators, otherwise remove them.

Hope that helps.

Upvotes: 1

Related Questions