0

I'm creating a form in Symfony2 that contains more than one submit button. I need to control where these buttons are rendered. I tried the following, but naturally nothing happens.

    <h1>Search Form</h1>
    <div id="search_form">
        {{ form(formSearch) }}
    </div>
    <div id="search_tasks">
        Tasks: 
        {{ form_widget(formSearch.searchButton1) }}
        {{ form_widget(formSearch.searchButton2) }}
    </div>

The search buttons are declared in the form class; they are rendered inside #search_form and nothing shows up in #search_tasks.

0

2 Answers 2

3

You are already rendering the whole form with {{ form(formSearch) }} (fields and buttons are only rendered once).

You need to render the start, rows and end separately.

{{ form_start(formSearch) }}
    <h1>Search Form</h1>
    <div id="search_form">
        {{ form_row(formSearch.field1) }}
        {{ form_row(formSearch.field2) }}
        {{ form_row(formSearch.field3) }}
    </div>
    <div id="search_tasks">
        Tasks: 
        {{ form_widget(formSearch.searchButton1) }}
        {{ form_widget(formSearch.searchButton2) }}
    </div>
{{ form_end(formSearch) }}
Sign up to request clarification or add additional context in comments.

2 Comments

I see, thank you. Is there a way to render multiple/all fields instead of one per row manually? The fields will are subject to occasional change so they can't be hardcoded.
You could use {% for child in form %} but then you would come into the same issues as above. The best bet would be to check whether the field is defined and then print that like {% if form.field is defined %}{{ form_row(form.field) }}{% endif %}.
3

I ran into the same issue where I needed my Submit and Reset buttons to be on the same row. My forms are dynamic so there was no way I could output the fields individually. I captured the buttons' HTML first so that form_widget(form) wouldn't output them for me.

{% form_theme form 'AppBundle::form/form_layout.html.twig' %}

<div class="row">

        {{ form_start(form) }}

        {% if form.submit is defined %}
                {% set submitButton = form_widget(form.submit) %}
        {% endif %}
        {% if form.reset is defined %}
                {% set resetButton = form_widget(form.reset) %}
        {% endif %}

        {{ form_widget(form) }}

        <div class="form-group row">                
                {% if submitButton is defined %}
                        {{ submitButton|raw }}
                {% endif %}
                {% if resetButton is defined %}
                        {{ resetButton|raw }}
                {% endif %}
        </div>

        {{ form_end(form) }}
</div>

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.