我有一个嵌入的表单,复合和inherit_data选项设置为true.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->setErrorBubbling(false);
$builder->add('date_start', 'date', array(
'label' => 'form.date_start.label',
'widget' => 'single_text',
'required' => false,
'group' => ['event', 'dates']
));
$builder->add('date_end', 'date', array(
'label' => 'form.date_end.label',
'widget' => 'single_text',
'required' => false,
'group' => ['event', 'dates']
));
$builder->add('time_zone', 'alternate_timezone', [
'label' => 'form.timezone.label',
'field_help' => 'form.timezone.help',
'empty_value' => 'form.timezone.empty_value',
'required' => false,
'group' => ['event', 'dates']
]);
}
/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults([
'label' => false,
'compound' => true,
'inherit_data' => true
]);
}
Run Code Online (Sandbox Code Playgroud)
我无法将错误与我的字段相关联.此表单的错误显示在父表单上,而不是字段上.我在doc中看到,除非表单是复合的,否则error_bubbling可能是false.
复合形式的解决方案是什么,有与该领域相关的错误?
谢谢
Aar*_*ser -1
使用 Symfony 中的复合表单 - 您可以为复合关系中的每个实体定义验证约束,验证期间产生的任何错误都将显示在发生错误的表单元素上方/附近。
例如 - 如果您发布的表单示例绑定到“ExampleEntity”实体 - 您可以在捆绑包的validation.yml 中定义该实体的验证约束。任何验证错误都将与其各自的子表单一起显示 - 即使在多个子表单上可能发生验证错误的集合中也是如此。
示例 src/MyBundle/Resources/Config/validation.yml
App\MyBundle\Entity\ExampleEntity:
properties:
date_start:
- NotBlank:
message: Date start cannot be blank.
date_end:
- NotBlank:
message: Date end field cannot be blank.
time_zone:
- NotBlank:
message: Timezone cannot be blank.
Run Code Online (Sandbox Code Playgroud)