Symfony2: Display Bootstrap Switch Button and get selected values in controller

I want to Implement following field in symfony2 . It have 2 Radio Buttons, and they are styled like Switch using some bootstrap styling.

Example here

above html and bootstrap snippet displays desired output

<div class="btn-group btn-toggle" data-toggle="buttons">
    <label class="btn btn-primary active">
      <input type="radio" name="options" value="option1"> On
    </label>
    <label class="btn btn-default">
      <input type="radio" name="options" value="option2" checked=""> Off
    </label>
  </div>

which outputs

for this example to work it require tag inside tag. but symfony form builder is rendering

<radio>..</radio>
<label></label>

how can I render <radio> tag inside <label> using symfony form builder?

In symfony for checkbox we use this code , but it displays normal radio group,

->add('check', 'choice', array(
                'choices' => array(
                    0 => 'On',
                    1 => 'Off'
                ),
                    'expanded'  => true,
                    'multiple'  => false,
                'data' => 1
            ))

Read this: http://symfony.com/doc/current/cookbook/form/form_customization.html

And look at this: https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig


in ProjectType.php

  $builder->add('progress', 'choice', array('label'=>'State of the project',
            'expanded' => true,
            'multiple' => false,
            'choices' => array(
                '1'=>'Draft',
                '2'=>'Advanced',
                '3'=>'Final step',
            )));

twig:

            <div class="form-group">
                {{ form_label(form.progress) }}
                <div class="col-sm-9 btn-group" data-toggle="buttons">
                    {% for key,choice in form.progress.vars.choices %}
                    <label class="btn btn-default {% if choice.value == form.progress.vars.value %}active{% endif %}">
                        <input type="radio" id="{{ form.progress.vars.id }}_{{ key }}" {% if choice.value == form.progress.vars.value %}checked{% endif %}
                               autocomplete="off" name="{{ form.progress.vars.full_name }}" value="{{ choice.value }}">
                        {{ choice.label }}
                    </label>
                    {% endfor %}
                </div>
            </div>
链接地址: http://www.djcxy.com/p/63150.html

上一篇: 如何在Symfony2和Wordpress之间共享会话

下一篇: Symfony2:显示Bootstrap开关按钮并获取控制器中的选定值