Zend form validation

后端 未结 3 1932
春和景丽
春和景丽 2020-12-11 19:18

I am using Zend Form to create dynamic form.

I have Zend Form validation too.

Trying to remove Validation dynamically, but not getting any success.

C

相关标签:
3条回答
  • 2020-12-11 19:27

    Your code is removing a validator by name

    $value->removeValidator('ele_4af42ceac7810');
    

    This is typically something like NotEmpty or Regex as opposed to the element name.

    Maybe you want

    $value->clearValidators();
    

    Also, +1 Ismael - you should remove it prior to calling $form->isValid()

    0 讨论(0)
  • 2020-12-11 19:33

    You should remove the validator BEFORE calling $form->isValid().

    0 讨论(0)
  • 2020-12-11 19:43

    The solution I came up with was overriding the isValid method on my form class and removing the validator on the password field if it was left blank:

    public function isValid($data)
    {
        if (empty($data['password'])) {
            $this->form
                 ->getElement('password')
                 ->clearValidators();
    
            $this->form
                 ->getElement('password_confirm')
                 ->clearValidators();
        }
        return $this->form->isValid($data);
    }
    

    The $this->form is referred to the form being extended by composition, the solution works when extending a Zend Form descendent class as well.

    Just a side note, this solution won't work if the password field is required

    0 讨论(0)
提交回复
热议问题