ecc ecc - 7 months ago 24
PHP Question

Symfony2: how to render form submit separately

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
.

Answer

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