[英]Symfony Form: CollectionType with different prototypes
編輯:盡管您的問題是關於CollectionType的,但在仔細查看屏幕截圖之后,我認為您只需要創建一個包含多個子表單的主表單,並根據用戶選擇顯示適當的子表單(使用javascript)即可。
如果您堅持使用CollectionType(我的屏幕快照中沒有顯示該原因),則可以執行以下操作:
$builder->add('entity1', CollectionType::class, [
'label' => 'entity.recipe.entity1',
'entry_type' => \AppBundle\Form\Type\Entity1Type::class,
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'prototype_name' => '__EntityId__',
'entry_options' => ['required' => false],
]);
$builder->add('entity2', CollectionType::class, [
'label' => 'entity.recipe.entity2',
'entry_type' => \AppBundle\Form\Type\Entity2Type::class,
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'prototype_name' => '__EntityId__',
'entry_options' => ['required' => false],
]);
...
根據需要添加任意多個實體類型,每個實體類型將是一個零個或多個項目的集合。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.