I want to display checkboxes from a pre-defined array in my Symfony form. User should be able to select more than one but I am not able to do it.
This is my code:
public function buildForm(FormBuilder $builder, array $options)
{
$roles = array('role1', 'role2', 'role3');
$builder
->add('name')
->add('roles', 'checkbox', $roles)
;
}
See the choice
type reference.
public function buildForm(FormBuilder $builder, array $options)
{
$roles = ['role1', 'role2', 'role3'];
$builder
->add('name')
->add('roles', 'choice', [
'choices' => $roles,
'multiple' => true,
'expanded' => true
])
;
}
You can use a choice
field instead:
public function buildForm(FormBuilder $builder, array $options)
{
$roles = array("role1","role2","role3");
$builder
->add('name')
->add('roles', 'choice', array(
'choices' => $roles,
'multiple' => true,
'expanded' => true,
))
;
}
Look at the documentation to know how you can have a checkbox, a select, or radio buttons with this field type: http://symfony.com/doc/current/reference/forms/types/choice.html#forms-reference-choice-tags
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