Search code examples
formssymfonyvalidationsymfony-3.3symfony3.x

Symfony form ChoiceType is ignoring the required attribute


On a form I add a field like this

$builder->add('cse',
ChoiceType::class,
array(
    'label' => '',
    'required' => true,
    'translation_domain' => 'messages',
    'choices' => array(
        'I agree' => true
    ),
    'expanded' => true,
    'multiple' => true,
    'data' => null,
    'attr' => array( 'class' => 'form_f' ),
)

)

While all other fields added to the form that have 'required' set to 'true' will prevent the form from being send the required attribute for this field is ignored (the form is sent anyways no matter if checked or not).

Do I have to handle this with Assert statements? If yes - still: why is required not working here?


Solution

  • Yes, use Assert.

    Because multiple=true print checkbox. Html validator can test radio, but no checkbox.

    Always use Assert for all forms, because html validator isn't safe :)