如何以symfony2形式生成多个复选框


How to generate multiple check boxes in symfony2 form

我想在Symfony表单中显示预定义数组中的复选框。用户应该可以选择多个,但我无法选择。

这是我的代码:

public function buildForm(FormBuilder $builder, array $options)
{
    $roles = array('role1', 'role2', 'role3');
    $builder
        ->add('name')
        ->add('roles', 'checkbox', $roles)
    ;
}

请参阅choice类型引用。

public function buildForm(FormBuilder $builder, array $options)
{
    $roles = ['role1', 'role2', 'role3'];
    $builder
        ->add('name')
        ->add('roles', 'choice', [
            'choices' => $roles,
            'multiple' => true,
            'expanded' => true
        ])
    ;
}

您可以使用choice字段:

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

查看文档,了解如何使用此字段类型的复选框、选择或单选按钮:http://symfony.com/doc/current/reference/forms/types/choice.html#forms-参考选择标签