How can I get required multiple checkboxes with the EntityType Field instead of a ChoiceType Field in Symfony3? Actually, I'm using:
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
->add('typesAdresses' , EntityType::class , array(
'class' => 'EKUserBundle:TypeAdresse',
'required' => true,
'expanded' => true,
'multiple' => true,
));
This will output multiple checkboxes but not as required.
In my form it must be required.
Checkboxes behaviour is different and you might get around it using the choice_attr option:
$builder
->add('typesAdresses' , EntityType::class , array(
'class' => TypeAddresse::class,
'expanded' => true,
'multiple' => true,
'choice_attr' => function($val, $key, $index) {
return array('required' => true);
},
))
;
However: I assume what you wish to achieve is “at least 1 checkbox checked in a group of checkboxes”. This is a rather different issue on its own and is more thoroughly explained in Using the HTML5 “required” attribute for a group of checkboxes? So you'll probably have to approach this with some JavaScript and leave out the required attributes in your FormType.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With