Search code examples
symfonysymfony-formssymfony-validator

Error for Constraint added to Field does not show for field


I dynamically generate a form and add constraints (i.e. 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']
        ])
    ]
]);

When submitting the form with empty data, the error shows up for the form, not the element of the form where i added the constraint (checked in profiler as well).

There is not Option atPath for those constraints and i add them directly to the field, so i do not get why they show up for the form.


Solution

  • That's what the error_bubbling option does (which you set to true in your form type):

    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.