Symfony 2.6 - render individual choice field (radio, checkbox) by name

11,853

Solution 1

Try this:

{% for key, transportItem in form.transport.children %}
    {{ form_widget(transportItem) }}
{% endfor %}

Solution 2

here my solution i used in my own project

{{form_label(form.transport)}}

 {% for key, item in form.transport.children %}
    <div class="custom-radio">
        <label for="{{item.vars.id}}">{{item.vars.label}}</label>
        <input 
            type="radio" 
            value="{{item.vars.value}}" 
            id=" {{item.vars.id}}" 
            name="{{item.vars.full_name}}" 
           {{ item.vars.checked ? 'checked' : '' }} 
        >
   </div>
{% endfor %}

or you can use a form theming

{% form_theme form _self %}

{%- block choice_widget_expanded -%}
    {%- for child in form %}
        <div class="custom-radio">
            {{- form_label(child) -}}
            {{- form_widget(child) -}}
        </div>
    {% endfor -%}
{%- endblock choice_widget_expanded -%}


{{form_widget(form)}}
Share:
11,853
grzechoo
Author by

grzechoo

Updated on July 24, 2022

Comments

  • grzechoo
    grzechoo almost 2 years

    How can I render an individual field (single radio/checkbox input field) in Twig in Symfony 2.6?

    Let's say I have a simple form:

    class TransportType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
                $builder->add('transport', 'choice', array(
                    'choices' => array(
                        'road' => 'Car/bus',
                        'train' => 'Train',
                    ),
                    'expanded' => true,
                    'multiple' => false
                ));
        }
    

    In previous Symfony2 versions I could just use:

    {{ form_widget(form.transport.road) }}
    {{ form_widget(form.transport.train) }}
    

    to render individual radio buttons, but it doesn't seem to work anymore. I know I can use:

    {{ form_widget(form.transport[0]) }}
    {{ form_widget(form.transport[1]) }}
    

    but it's less flexible. Of course I can iterate over the collection and check for name, but this seems like unnecessary hassle. Isn't there an easier way?

    I tried offsetGet (which is supposed to return a child by name), but it also works only with array index.

  • Ferrybig
    Ferrybig about 8 years
    While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
  • Stormnorm
    Stormnorm over 3 years
    This doesn't work as the ChoiceView does not have id and full_name properties. github.com/symfony/form/blob/5.x/ChoiceList/View/ChoiceView.‌​php I think you would have to take the name from the FormView object and build the id from the id from the FormView with the key of the current choice view. Something like: form.transport.id ~'_'~ key for the id and from.transport.full_name as the name will be the same for all choices.