简体   繁体   English

Symfony:具有预选值的choice_list

[英]Symfony: choice_list with pre-selected values

I have an array field in my Entity called Type and I want to restrict the value that the user can specify for the field as this: 我的实体中有一个名为Type的数组字段,我想限制用户可以为此字段指定的值:

Type1      [] subtitle1        [] subtitle2         [] subtitle3

Type2      [] subtitle1        [] subtitle2         [] subtitle3

I managed to do this by creating choice form type and a little twig customization like this: 我通过创建choice表单类型和一些小树枝定制来做到这一点:

$form = $this->createFormBuilder($entity)
                ->add('name', 'text')
                ->add('type', 'choice', array(
                    'multiple' => true,
                    'choice_list' => new myBundle\Form\Extension\CustomChoiceList($param1,$param2),
                    'label' => 'my Label',
                    'expanded' => true
                ));// CustomChoiceList extends ChoiceList

my issue now is when i have an entity i want to edit, how can i show the user the same form but with some of the checkboxes checked? 现在的问题是,当我有一个要编辑的实体时,如何向用户显示相同的表单,但其中某些复选框已选中?

I checked ChoiceList and it's creating the checkboxes by using ChoiceView class which has no checked option only label , value , data 我检查了ChoiceList并使用ChoiceView类创建了复选框,而ChoiceView类仅具有labelvaluedata而不具有checked选项

Thanks 谢谢

You can set the pre selected checkboxes with the "data" property 您可以使用“数据”属性设置预先选择的复选框

$form = $this->createFormBuilder($entity)
                ->add('name', 'text')
                ->add('type', 'choice', array(
                    'multiple' => true,
                    'choice_list' => new myBundle\Form\Extension\CustomChoiceList($param1,$param2),
                    'label' => 'my Label',
                    'expanded' => true,
                    'data' => 0 // Checks the first choise
                ));// CustomChoiceList extends ChoiceList

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM