Zend form validation

て烟熏妆下的殇ゞ 提交于 2019-11-27 07:52:13

问题


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.

Can you plz help me to remove Zend Validation.

Bellow is my code for remove validation :

$toRemValArray = array();
$toRemValArray[0] = 'ele_4af42ceac7810';

if(isset($_POST['btnPost_x'])){                 
    if ($form->isValid($_POST)) {
        $allElements = $form->getElements();
        foreach($allElements as $val){
            if(in_array('ele_4af42ceac7810',$toRemValArray)){
                $value = $form->getElement($val->getName());
                $value->removeValidator('ele_4af42ceac7810');
            }
        }
    }
}

Let me know whether my code is having any issue

Thanks Mohammad Tareque


回答1:


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




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/1707002/zend-form-validation

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