Symfony2中的组表单字段 [英] Group form fields in Symfony2

查看:44
本文介绍了Symfony2中的组表单字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对symfony2中的字段进行分组.例如,将它们包装在div中,然后在它们之间放置标题:

I want to group fields in symfony2. For example wrap them in a div and place headlines in between:

<form>
<div class="step-1">
    <h3>Step 1</h3>
    Field 1
    Field 2
</div>
<div class="step-2">
    <h3>Step 2</h3>
    Field 3
    Field 4
</div>
</form>

问题在于表单中包含很多字段,因此我无法在模板中一一呈现它们.添加字段时没有任何选择吗?喜欢:

The problem is the form got very much fields so i cant render them one by one in the template. Isnt there any option when adding fields? Like:

$form = $this->createFormBuilder()
        ->addGroup('step-1')

或者我该如何处理?

推荐答案

Twig可以帮助您以最少的代码显示多个字段:

Twig can help you to display multiple fields with a minimal code:

<form>
<div class="step-1">
    <h3>Step 1</h3>
    {% for field in [ 'field1', 'field2']
        if (attribute(form, field) is defined) %}
        {{ form_row(attribute(form, field)) }}
    {% endfor %}
</div>
<div class="step-2">
    <h3>Step 2</h3>
    {% for field in [
        'field3',
        'field4',
        'field5',
        'field6'
    ] if (attribute(form, field) is defined) %}
        {{ form_row(attribute(form, field)) }}
    {% endfor %}
</div>

{# Display the other fields #}
{{ form_rest(form) }}

</form>

这篇关于Symfony2中的组表单字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆