How to customize the data-prototype attribute in Symfony 2 forms

后端 未结 11 1148
梦如初夏
梦如初夏 2020-12-12 11:35

Since umpteens days, I block on a problem with Symfony 2 and forms.

I got a form of websites entities. \"Websites\" is a collection of website\'s entities and each w

11条回答
  •  离开以前
    2020-12-12 12:07

    I've run into similar problem recently. Here's how you can override the collection prototype without having to explicitly set it in the html:

    {% set customPrototype %}
        {% filter escape %}
            {% include 'AcmeBundle:Controller:customCollectionPrototype.html.twig' with { 'form': form.collection.vars.prototype } %}
        {% endfilter %}
    {% endset %}
    {{ form_label(form.collection) }}
    {{ form_widget(form.collection, { 'attr': { 'data-prototype': customPrototype } }) }}
    

    You can do whatever you want then in your custom twig. For example:

    {{ form_label(form.field0) }} {{ form_widget(form.field0) }}
    {{ form_label(form.field1) }} {{ form_widget(form.field1) }}

    Useful when you only have to do it in specific places and don't need a global override that's applicable to all collections.

提交回复
热议问题