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.
Try this:
{% for key, transportItem in form.transport.children %}
{{ form_widget(transportItem) }}
{% endfor %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With