Symfony: choice_list with pre-selected values

戏子无情 提交于 2019-12-14 04:22:44

问题


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?

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

Thanks


回答1:


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


来源:https://stackoverflow.com/questions/20039511/symfony-choice-list-with-pre-selected-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!