How to enable multiple choice in symfony2 form field: collection
10,320
You don't need the collection
type here. The choice
type with "multiple" set to true
will output a collection just fine.
$builder
->add('roles', 'choice', array(
'expanded' => true,
'multiple' => true,
'choices' => array(
'ROLE_CONTENT' => 'Innehåll',
'ROLE_LAYOUT' => 'Skärmlayout',
'ROLE_VIDEO' => 'Videouppladdning',
'ROLE_ADMIN' => 'Administratör',
),
))
;
Author by
Matt Welander
Updated on June 04, 2022Comments
-
Matt Welander almost 2 years
I have a user registration form (that the admin uses) and I want to include the choices for role. I have successfully added a drop-down of roles with the following code in my buildForm function:
$builder ->add('roles', 'collection', array( 'type' => 'choice', 'options' => array( 'choices' => array( 'ROLE_CONTENT' => 'Innehåll', 'ROLE_LAYOUT' => 'Skärmlayout', 'ROLE_VIDEO' => 'Videouppladdning', 'ROLE_ADMIN' => 'Administratör', ), ), ));
Now, I want this to be a multichoice or list of checkboxes instead of a dropdown, but I can't seem to find a way, seems like a simple enough thing to do or am I missing something about how this field type works, can it not take multiple choices?
-
mmoreram over 7 yearsThis is not true. Collection extends FormType, not ChoiceType. symfony.com/doc/current/reference/forms/types/collection.html