phpcsstwitter-bootstrapsymfonyformbuilder

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


I want to Implement the following field in symfony2. It has 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

preview of this

for this example to work it requires 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
            ))

Solution

  • 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