How to generate multiple check boxes in symfony2 form

匿名 (未验证) 提交于 2019-12-03 08:48:34

问题:

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)     ; } 

回答1:

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         ])     ; } 


回答2:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!