Zend form validation

流过昼夜 提交于 2019-11-28 13:48:29

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

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

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()

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!