如何为Symfony表单中的实体分组复选框?
->add(
'products',
EntityType::class,
[
'class' => Product::class,
'choice_label' => 'titel',
'multiple' => true,
'expanded' => true,
]
)
我的域模型Product
只属于一个ProductFamily
。每个ProductFamily
只属于一个ProductSuperFamily
。在我的表单中,Product
复选框应按如下方式分组:
Product Super Family 1
Product Family 1
[ ] Product 1
[ ] Product 2
Product Family 2
[ ] Product 3
Product Family 3
[ ] Product 4
[ ] Product 5
Product Super Family 2
Product Family 4
[ ] Product 6
[ ] Product 7
[ ] Product 8
Product Family 5
[ ] Product 9
Product Super Family 3
Product Family 6
[ ] Product 10
[ ] Product 11
我怎么能做到这一点?
答案 0 :(得分:0)
选择类型允许,您必须使用the group_by
option:
'group_by' => function(Product $product) {
return $product->getFamily()->getLabel()
},