7

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

这是我的代码:

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

2 回答 2

9

请参阅choice类型参考

public function buildForm(FormBuilder $builder, array $options)
{
    $roles = ['role1', 'role2', 'role3'];

    $builder
        ->add('name')
        ->add('roles', 'choice', [
            'choices' => $roles,
            'multiple' => true,
            'expanded' => true
        ])
    ;
}
于 2012-07-13T07:58:06.997 回答
5

您可以改用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-reference-选择标签

于 2012-07-13T08:00:02.300 回答