[英]Select with optgroup in Symfony 2.0
In Symfony2 , the select
html component is rendered as a ChoiceType
object, which is used indeed also for checkboxes
and radiobuttons
. 在Symfony2中 ,
select
html组件呈现为ChoiceType
对象,该对象的确也用于checkboxes
和checkboxes
radiobuttons
。
Does someone really know how to render a select with the optgroup option in Symfony2 ? 有人真的知道如何使用Symfony2中的optgroup选项呈现选择吗?
For sake of completeness, here I report an example of a select with the optgroup tag (example from w3cschools ): 为了完整起见,我在这里报告一个带有optgroup标记的select示例(来自w3cschools的示例):
<select>
<optgroup label="Swedish Cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
</optgroup>
<optgroup label="German Cars">
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</optgroup>
</select>
Moreover, notice that there is a similar post here , but the answers are not convincing. 此外,通知有一个类似的帖子在这里 ,但答案是没有说服力的。
Do this: 做这个:
$car_choices = array(
'Swedish Cars' => array(
'volvo' => 'Volvo',
'saab' => 'Saab',
),
'German Cars' => array(
'mercedes' => 'Mercedes',
'audi' => 'Audi'
)
);
$form = $this->createFormBuilder()
->add('car', 'choice', array(
'label' => 'Choose your car',
'choices' => $car_choices,
))
->getForm();
Works for Symfony 2.0.x 适用于Symfony 2.0.x
It depends how your Entity is defined and how you group your entity. 这取决于如何定义实体以及如何对实体进行分组。 Assuming the grouping is done given a parameter in your object, let's say "brand".
假设在给定对象中的参数的情况下完成了分组,我们称“品牌”。 You can do:
你可以做:
$builder->add('cars', null, array(
'group_by'=> 'brand'
));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.