繁体   English   中英

Symfony:具有预选值的choice_list

[英]Symfony: choice_list with pre-selected values

我的实体中有一个名为Type的数组字段,我想限制用户可以为此字段指定的值:

Type1      [] subtitle1        [] subtitle2         [] subtitle3

Type2      [] subtitle1        [] subtitle2         [] subtitle3

我通过创建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

现在的问题是,当我有一个要编辑的实体时,如何向用户显示相同的表单,但其中某些复选框已选中?

我检查了ChoiceList并使用ChoiceView类创建了复选框,而ChoiceView类仅具有labelvaluedata而不具有checked选项

谢谢

您可以使用“数据”属性设置预先选择的复选框

$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