3

How can I access a field in collection in twig

$builder
->add('name', 'text', array('required' => false))
->add('email', 'collection', array(
    'type'      => new UsersEmailAddressesType(),
    'allow_add' => true
))

UserEmailAddressesType has two fields name and email, how can I access email field in twig ?

4

1 回答 1

4

在 symfony 食谱中有一个关于如何在表单中嵌入集合的示例。那里的解决方案如下所示(适用于您的表单示例):

<ul>
{% for email in form.email %}
    <li>{{ form_row(email.address) }}</li>
{% endfor %}
</ul>

由于您想将输入彼此相邻放置,您可能需要检查 loop.index 是否为oddeven或者divisibleby()例如像这样:

{% for email in form.email %}
    {% if loop.index is odd %}
        <li class="float-left">
    {% else %}
        <li class="float-right">
    {% endif %}
    {{ form_row(email.address) }}</li>
{% endfor %}
于 2013-05-28T13:56:23.147 回答