【发布时间】:2016-04-18 19:00:24
【问题描述】:
我的表单女巫使用自定义非映射子表单。
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('subtype', 'my-subtype');
}
}
子表单由多个字段组成,我需要同时对它们进行额外检查。回调约束非常适合这项工作。但是我找不到如何在整个子窗体上添加此约束的方法。
到目前为止,我已尝试在 setDefaultOptions() 中设置回调或在 buildForm() 中使用 setAttribute() 进行设置,但未评估回调。
目前我只是将回调添加到其中一个字段:
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('field1', 'text')
->add(
'field2', 'text',
array(
'constraints' => array(
new Callback(array(
'methods' => array(array($this, 'validateMyType'))
))
)
));
}
public function validateMyType($data, ExecutionContextInterface $context) {
// Validation failed...
$context->addViolationAt('subtype', "mySubtypeViolation");
return;
}
然而,这阻止了我在整个子类型上添加违规。无论我在 addViolationAt() 中使用什么,都会将违规添加到承载回调约束的字段中。
【问题讨论】:
标签: php forms validation symfony constraints