Symfony - checkboxtype

时间:2016-06-06 15:57:43

标签: symfony checkbox

我在symfony框架中遇到了checkboxtype的问题。我创建表单的代码是:

$form = $this->createFormBuilder()
            ->add('table_records', CheckboxType::class)
            ->add('save', SubmitType::class, array('label' => 'delete'))
            ->getForm();

我的问题是复选框中的数组,因为我需要checkboxname例如table_records [],用于许多复选框。

3 个答案:

答案 0 :(得分:4)

使用ChoiceType并将expanded设置为true

$builder->add('table_records', ChoiceType::class, array(
    'choices' => array(),
    'expanded' => true
)

答案 1 :(得分:2)

像这样,使用ChoiceType是正确的方法。

$form = $this->createFormBuilder()
        ->add('table_records', ChoiceType::class, array())
        ->add('save', array('label' => 'delete'))
        ->getForm();

答案 2 :(得分:0)

从你的问题不清楚你是否需要:

1。多个复选框组,不会按您的要求命名

2。 Checkbox的集合,每个都将以索引号

命名

<强> 1 结果示例:

<form> 
    <input type="checkbox" name="table_records[]" value="1"/>
    <input type="checkbox" name="table_records[]" value="2"/>
    ...
</form>

您需要:

    ->add('table_records',ChoiceType::class,[
        'multiple'=>true,
        'expanded'=>true,
        'choices'=>[
            'label1'=>'value1',
            'label2'=>'value2'
        ]
    ])

看看multiple + expanded这两个都是正确的,这将使表单内的ChoiceType渲染复选框。

2。结果示例:

<form> 
    <input type="checkbox" name="table_records[0]" value="1"/>
    <input type="checkbox" name="table_records[1]" value="2"/>
    ...
</form>

然后你需要:

->add('table_records',CollectionType::class,[
    'entry_type'=>CheckboxType::class,
    'entry_options'=>[ //Options for CheckboxType goes here
        '...'
    ]
])