为什么自定义验证中的错误会限制未附加到表单的特定字段? [英] Why errors from custom validation constrain not attached to specific field by to the form?
问题描述
我有一个 ArticelPattern 类,它具有 $extraPatterns 属性.此属性是一个 ExtraPattern(类)元素数组.
I have an ArticelPattern class which has $extraPatterns property. This property is an array of ExtraPattern (class) elements.
ExtraPattern 类有一个自定义约束(我使用注释应用).
There is a custom constraint on the class ExtraPattern (which I apply using annotation).
为了处理表单,我创建了一个带有 extraPatterns 集合字段的 ArticelPatternType 类.
For form handling I created an ArticelPatternType class with extraPatterns collection field.
现在,当我验证此表单并且 ArticelPattern::$extraPatterns 集合的元素之一引发约束违规时,错误未分配给该元素,而是分配给整个表单,因此我无法在导致此错误的相应表单字段旁边显示此错误.
Now, when I validate this form and there is a constraint violation raised by one of the elements of the ArticelPattern::$extraPatterns collection, the error isn't assigned to that element, but instead to the whole form, so I can't display this error beside the appropriate form field which caused this error.
而不是将我的自定义约束放在 ExtraPattern 类上,如下所示:
Instead of putting my custom constraint on the ExtraPattern class, like this:
/**
* @AssertApl
*/
class ExtraPattern
我也试过把它放在集合属性上,像这样:
I also tried putting it on the collection property, like this:
/**
* @Assert\All(@AssertApl)
*/
private $extraPatterns;
但结果相同.由这个集合的元素引起的@AssertApl 约束错误不是分配给它们,而是分配给整个表单.
But with the same result. @AssertApl constraint errors caused by elements of this collection are not assigned to them, but to the whole form.
知道如何让它工作吗?
推荐答案
你的类约束可能被映射到一个 form
类型的表单,它有 error_bubbling
启用默认.将此选项设置为 false
应该可以满足您的要求.
You class constraint is probably being mapped to a form of form
type, which has error_bubbling
enabled by default. Setting this option to false
should do what you're looking for.
$builder ->add('extraPatterns', 'collection', array(
// ...
'options' => array('error_bubbling' => false),
));
这篇关于为什么自定义验证中的错误会限制未附加到表单的特定字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!