3

I want to achive this is twig:

{{ form_widget(form.orderItems.0.enabled) }}
{{ form_widget(form.orderItems.1.enabled) }}
{{ form_widget(form.orderItems.2.enabled) }}

....

but the number to be a variable.

I tried this:

{% set index = 0 %}
{{ form_widget(form.orderItems.index.enabled) }}

Error: Method "index" for object "Symfony\Component\Form\FormView" does not exist

and this:

{{ form_widget(form.orderItems.{{index}}.enabled) }}

Error: Expected name or number

and this:

{{ form_widget(form.orderItems.~index~.enabled) }}
Error: Expected name or number

It is possible to achieve this :(

2
  • Have you tried {{ form_widget(form.orderItems.[index].enabled) }} ? Just a theory. Commented Aug 12, 2013 at 10:14
  • Yes :( - Expected name or number Commented Aug 12, 2013 at 10:17

2 Answers 2

3

Some digging suggests you use the 'attribute' function - see Accessing array values using array key from Twig.

I suppose that would be something like

form_widget(attribute(form.orderItems, index).enabled)

Unfortunately I can't easily test that at the moment, but it should get you on the right track.

Sign up to request clarification or add additional context in comments.

Comments

1

I am solving the same problem now and getting "expected name or number error" when I want to access variable dynamically. I can not find simple answer, how to dynamically replace some part of variable in twig. But It works without first dot as was first comment here.

{{ form[othervariable value].vars.label }}

and NOT

{{ form.[value].name }}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.