rest command showing previously

I have a form with a collection type field, rendered like that:

<div id="beneficiosTab" class="opcional">
    Beneficios
    <ul class="beneficios" data-prototype="{{ form_widget(formAtendimento.beneficios.get('prototype')) | e }}">
        {% for beneficio in formAtendimento.beneficios %}
            <li>{{ form_row(beneficio.coTipoBeneficio) }}</li>
            <li>{{ form_row(beneficio.vrValor) }}</li>
            <li>{{ form_row(beneficio.boConcedido) }}</li>
        {% endfor %}
        <li><a href="#" id="addBeneficio">Add Beneficio</a></li>
    </ul>
</div>
<div style="clear:both"></div>
{{ form_rest(formAtendimento) }}

The form's entity can have multiple items of the collection, or none.

When the entity has items of the collection, it works fine, but when it has none, the "for" in the twig doesn't happen, and a "Beneficios" div is generated in form_rest.

Any way I can prevent that? Thanks in advance.


This seems like a bug in the form rendering. I managed to disable the extra form rendering in the form_rest function, by adding this code just after rendering the collection elements:

{% do form.uploads.setRendered() %}

Where "uploads" is my collection field type. This doesn't seem like best practice to me though.

So the whole rendering looks like this:

 <div id="uploads" data-prototype="{{ form_widget(form.uploads.vars.prototype)|e }}">
        {% for upload in form.uploads %}
            {{ form_widget(upload) }}
        {% endfor %}
  </div>
  {% do form.uploads.setRendered() %}

form_rest generate all unrendered forms. Every input is form in Symnfony2, the same goes with collection type.

You never printed out collection, so Symfony makes it for you. If you want hide it, and still use form_rest simply print it it to:

<div style="display: none" />
链接地址: http://www.djcxy.com/p/81392.html

上一篇: Symfony 2与类型文件的表单集合字段

下一篇: 先前显示的休息命令