添加到字段的约束错误不显示字段

Error for Constraint added to Field does not show for field

我动态生成一个表单并添加约束(即 Choice)。

$builder->add('test', 'choice', [
    'choices' => [1, 'one', 2 => 'two'],
    'required' => true,
    'expanded' => true,
    'error_bubbling' => true,
    'cascade_validation' => true,
    'label' => 'this_is_a_test',
    'multiple' => false,
    'constraints' => [
        new NotBlank([
            'groups' => ['Default']
        ]),
        new Choice([
            'min' => 1,
            'choices' => [1, 2],
            'groups' => ['Default']
        ])
    ]
]);

当提交包含空数据的表单时,错误显示的是表单,而不是我添加约束的表单元素(也在探查器中检查过)。

这些约束没有选项 atPath,我将它们直接添加到字段中,所以我不明白为什么它们会出现在表单中。

这就是 error_bubbling 选项的作用(您在表单类型中将其设置为 true):

If true, any errors for this field will be passed to the parent field or form. For example, if set to true on a normal field, any errors for that field will be attached to the main form, not to the specific field.