Symfony:具有预选值的choice_list [英] Symfony: choice_list with pre-selected values

查看:184
本文介绍了Symfony:具有预选值的choice_list的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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:

$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?

我检查了 ChoiceList ,它使用 ChoiceView 只有选项选项标签 >值数据

I checked ChoiceList and it's creating the checkboxes by using ChoiceView class which has no checked option only label, value, data

谢谢

推荐答案

您可以使用data属性设置预先选中的复选框

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

这篇关于Symfony:具有预选值的choice_list的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆