本文介绍了在Symfony 2.0中选择optgroup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Symfony2 中, select html组件呈现为 ChoiceType 对象,这也确实用于复选框单选按钮



是否真的知道如何使用Symfony2中的optgroup选项呈现选择?为了完整起见,我在这里报告了一个 select 与 optgroup 标记的示例(示例来自 a href =http://www.w3schools.com/tags/tag_optgroup.asp =nofollow noreferrer> w3cschools ):

 <选择> 
< optgroup label =瑞典汽车>
< option value =volvo>沃尔沃< / option>
< option value =saab> Saab< / option>
< / optgroup>
< optgroup label =德国汽车>
< option value =mercedes> Mercedes< / option>
< option value =audi> Audi< / option>
< / optgroup>
< / select>

此外,请注意,有一个类似的帖子,但答案并不令人信服。

解决方案

执行此操作:

 <$ c $ (
'volvo'=>'沃尔沃',
'saab'=>'萨博',$ b (
'mercedes'=>'奔驰',
'audi'=>'奥迪'
)$($ b $)
'German Cars'=> b $ b);
$ b $ form = $ this-> createFormBuilder()
- > add('car','choice',array(
'label'=>'Choose你的车',
'选择'=> $ car_choices,
))
- > getForm();

适用于Symfony 2.0.x


In Symfony2, the select html component is rendered as a ChoiceType object, which is used indeed also for checkboxes and radiobuttons.

Does someone really know how to render a select with the optgroup option in Symfony2?

For sake of completeness, here I report an example of a select with the optgroup tag (example from 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中选择optgroup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 07:08