3

I am a newbie to zend framework. I am developing a simple user registration application in zend. I have both add and edit functionalities in my application. I can add a new user perfectly using my application. But when I try to edit user information other than password the validators used for password and passwordConfirmation fields gives errors. I need to remove/disable validators of password and passwordConfirmation fields only when I want to edit the user informations other than password.

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(2, 20, 'messages'=>'password must be 2-20 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 do not match'))
        ->addValidator(new Application_Validate_PasswordConfirmation())
        ->setLabel('Retype password');

My validator class:

class Application_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;
     }
}
minhaz
  • 503
  • 1
  • 5
  • 12

2 Answers2

4
    $modelUsers = new Model_Users();
    $userId = $this->_getParam('userId');

    if ($userId) {
        $populateData = array();

        $user = $modelUsers->fetch($userId);

        if ($user instanceof Model_User) {
            $populateData = $user->toArray();
        }
        $form = $this->_getAddForm($user->email, $user->username, $user->phone);
        $password = $this->getRequest()->getPost('password');
        if (trim($password) == '') {
            $form->getElement('password')
            ->setRequired(false)
            ->clearValidators();

            $form->getElement('confirmPassword')
            ->setRequired(false)
            ->clearValidators();
        }

    }
   else {
        $form = $this->_getAddForm($mail=null, $username=null, $phone=null);
   }
    $request = $this->getRequest();

    if ($request->isPost()) {

        $post = $request->getPost();
        if ($form->isValid($post)) {
            $values = $form->getValidValues($post);

This is how I solve this problem definitely with the help of @drew010

user1559230
  • 2,790
  • 5
  • 26
  • 32
3

When the form is submitted in edit mode, remove the validators using the following code prior to calling $form->isValid():

$password = $this->getRequest()->getPost('password');
if (trim($password) == '') { // password field empty, remove validators
    $form->getElement('password')
         ->setRequired(false)
         ->clearValidators();

    $form->getElement('confirmPassword')
         ->setRequired(false)
         ->clearValidators();
}

if ($form->isValid($this->getRequest()->getPost())) {
    // ...
}
drew010
  • 68,777
  • 11
  • 134
  • 162
  • ,How can I recognize that form is submitted in edit mode. I don't see any difference between edit and submit except form population.As I am very new to zend framework can you explain it just a little bit. Thanks – minhaz Sep 23 '12 at 16:32
  • In one instance you have no data and need to create a user, in the other you have to pre-populate it and presumably the person is logged in? – drew010 Sep 23 '12 at 16:35
  • Yeah, But I populate the form from the controller.Should I add your code in the controller – minhaz Sep 23 '12 at 16:39
  • Yes probably in the controller, wherever you are currently validating the form. – drew010 Sep 23 '12 at 16:44
  • I'm validation the form in Application_Validate_PasswordConfirmation I have posted the class Application_Validate_PasswordConfirmation in my question. Can you tell me how can I use your code in that class – minhaz Sep 23 '12 at 16:48
  • Application_Validate_PasswordConfirmation is just a validator for one purpose. You don't want the code I posted there. It should be in the controller, or just before wherever you are currently calling $form->isValid – drew010 Sep 23 '12 at 16:52