Zend Framework 2-删除的表单元素导致验证失败 [英] Zend Framework 2 - Removed form element causes validation to fail
问题描述
我在某些地方使用某种形式.在其中之一中,我需要忽略在验证后以编程方式设置的表单元素.
I use a certain form in several places. In one of them I need to ignore a form element which I set programmatically after the validation.
因为这只是一个例外,所以我不想创建一个新表单.所以我想,我只需在控制器中删除此元素,如:
Because it's just an exception I don't want to create a new form. So I thought, I just remove this element in the controller like:
$myForm->remove('myElement');
问题在于该表单现在无法验证.我没有收到任何错误,但是$myForm->isValid()
只是返回一个空值.
The problem is that the form now won't validate. I don't get any errors but the $myForm->isValid()
just returns an empty value.
有什么想法我可能做错了吗?
Any ideas what I might be doing wrong?
谢谢!
推荐答案
Ok, finally I found a solution! You can define a ValidationGroup
which allows you to set the attributes you'd like to validate. The others are not validated:
$form->setValidationGroup('name', 'email', 'subject', 'message');
$form->setData($data);
if ($form->isValid()) {
...
这篇关于Zend Framework 2-删除的表单元素导致验证失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!