Reputation: 4117
I want to render dynamicly a form in Symfony. I passing a array with elements of element names to the render method 'formElements' => array('formelement1', 'formelement2')
.
How i want to use the element names in my template to show the form labels.
{% for elementName in elementNames %}
<div class="form-lable">
{{ form_label({{ elementName }}) }}
</div>
{% endfor %}
I received the following exception:
A hash key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "punctuation" of value "{" in onBillBundle:Customer:new.html.twig at line 17
Is it not posible to render the form dynamicly without {{ form(delete_form) }}
?
Upvotes: 1
Views: 1745
Reputation: 2811
Untested...
{% for elementName in elementNames %}
<div class="form-lable">
{{ form_label(attribute(form, elementName)) }}
</div>
{% endfor %}
Upvotes: 2
Reputation: 6410
You don't need to surround variables in a twig statement with {{
and }}
. So your code should be:
{{ form_label(elementName) }}
But of course elementName
needs to be a Form Object and not a string. You can generate them in your Controller like this:
public function testAction()
{
// ...
$form = $this->createFormBuilder()
->add('name', 'text');
return ['form' => $form->createView()];
}
Upvotes: -1