Search code examples
formssymfonyprototypetwigarraycollection

How to customize data-prototype in symfony2 form collections?


I've got a collection of hidden fields in my form.

<ul id="user_roles">
  <li><hidden field value="role1"></li>
  <li><hidden field value="role2"></li>
  (...)
</ul>

I use jQuery (and data-prototype) to add new roles.

The problem is that I would like to render something like this:

<ul id="user_roles">
  <li>role1 <hidden field value="role1"></li>
  <li>role2 <hidden field value="role2"></li>
  (...)
</ul>

No problem with the initial rendering: i just put:

{% for role in roles %}
 <li> {{ role }} {{ form_row(role) }} </li>
{% endfor %}

But the default data-prototype will render only {{ form_row(role) }} (a hidden field).

Where am I supposed to change the default data-prototype?

There is no {% block prototype %} in form_div_layout.html that i could customize....


Solution

  • The collection widget is defined as follows:

    {% block collection_widget %}
    {% spaceless %}
        {% if prototype is defined %}
            {% set attr = attr|merge({'data-prototype': form_row(prototype) }) %}
        {% endif %}
        {{ block('form_widget') }}
    {% endspaceless %}
    {% endblock collection_widget %}
    

    So you can override this to gain control on how you want to rendre the prototype.