可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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