简体   繁体   English

Symfony2:如何呈现表单分别提交

[英]Symfony2: how to render form submit separately

I'm creating a form in Symfony2 that contains more than one submit button. 我正在Symfony2中创建一个包含多个提交按钮的表单。 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 . 在渲染内#search_form并没有在显示出来#search_tasks

You are already rendering the whole form with {{ form(formSearch) }} (fields and buttons are only rendered once). 您已经使用{{ form(formSearch) }}渲染了整个表单(字段和按钮仅被渲染一次)。

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

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. 我首先捕获了按钮的HTML,以便form_widget(form)不会为我输出它们。

{% 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM