0
votes

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.

2

2 Answers

3
votes

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) }}
2
votes

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>