[英]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
類僅具有label
, value
, data
而不具有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.