1

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) }}?

2 Answers 2

2

Untested...

{% for elementName in elementNames %}
    <div class="form-lable">
        {{ form_label(attribute(form, elementName)) }}
    </div>
{% endfor %}

Docs

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

Comments

-1

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()];
}

1 Comment

yes, i know... But i want so place the form elements by my own in the twig file like this: symfony.com/doc/current/cookbook/form/form_customization.html

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.