douzhan2027 2016-12-20 09:06
浏览 42
已采纳

选择Symfony表单视图中的所有复选框

In symfony form I have some choices, checkboxes.

$form->add('keywords', ChoiceType::class, array(
            'choices' => $keywords,
            'label' => 'With following keywords',
            'expanded' => true,
            'multiple' => true,
        ));

I need all checboxes selected when form is initialised. How I can do that if possible in symfony way or I will need to do that with JavaScript?

  • 写回答

1条回答 默认 最新

  • dongyuji7309 2016-12-20 09:24
    关注

    Just add choice_attr

    $form->add('keywords', ChoiceType::class, array(
                'choices' => $keywords,
                'label' => 'With following keywords',
                'expanded' => true,
                'multiple' => true,
                'choice_attr' => function() {
                    return ['checked' => 'checked'];
                },
            ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?