Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cowlby
Created October 17, 2011 23:32
Show Gist options
  • Star 32 You must be signed in to star a gist
  • Fork 14 You must be signed in to fork a gist
  • Save cowlby/1294186 to your computer and use it in GitHub Desktop.
Save cowlby/1294186 to your computer and use it in GitHub Desktop.
Customizing form collections in Symfony2
{% block collection_widget %}
{% spaceless %}
<div class="collection">
{% if prototype is defined %}
{% set attr = attr|merge({'data-prototype': block('collection_item_widget') }) %}
{% endif %}
<div {{ block('widget_container_attributes') }}>
{{ form_errors(form) }}
<ul>
{% for rows in form %}
<li>
{% set fieldNum = 1 %}
{% for row in rows %}
<div class="field{{ fieldNum }}">
{{ form_label(row) }}
{{ form_widget(row, { 'attr': { 'class': 'test' }}) }}
</div>
{% set fieldNum = fieldNum + 1 %}
{% endfor %}
<a class="remove" title="Remove" href="javascript:void()">
<span>Delete</span>
</a>
<div class="clear"></div>
</li>
{% endfor %}
</ul>
{{ form_rest(form) }}
</div>
<div class="clear"></div>
<a class="add" title="Add" href="javascript:void()">
<div style="display: none;"></div>
<span>Add</span>
</a>
</div>
<div class="clear"></div>
{% endspaceless %}
{% endblock collection_widget %}
{% block collection_item_widget %}
{% spaceless %}
<li>
{% set fieldNum = 1 %}
{% for row in prototype %}
<div class="field{{ fieldNum }}">
{{ form_label(row) }}
{{ form_widget(row, { 'attr': { 'class': 'test' }}) }}
</div>
{% set fieldNum = fieldNum + 1 %}
{% endfor %}
<a class="remove" title="Remove" href="javascript:void()">
<span>Delete</span>
</a>
<div class="clear"></div>
</li>
{% endspaceless %}
{% endblock collection_item_widget %}
@ujaladh
Copy link

ujaladh commented Mar 4, 2014

How can I use collection_widget to render view? Can you please give one simple usage? I am trying to use this for TagCollection in Task and Tag tutorial of Symfony.

@sn00011
Copy link

sn00011 commented Jun 30, 2014

Hi @ujaladh, here is a link that might help.

@Looted
Copy link

Looted commented Dec 27, 2014

When I try to use it with the symfony 2.6 bootstrap form theme, I get "Variable "widget" does not exist in bootstrap_3_layout.html.twig at line 169". The code responsible is below (fourth from bottom). Any idea how to fix this? Thanks.

{% block checkbox_radio_label %}
{% if required %}
{% set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' required')|trim}) %}
{% endif %}
{% if parent_label_class is defined %}
{% set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' ' ~ parent_label_class)|trim}) %}
{% endif %}
{% if label is empty %}
{% set label = name|humanize %}
{% endif %}
<label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>
{{ widget|raw }}
{{ label|trans({}, translation_domain) }}

{% endblock checkbox_radio_label %}

@nicolaskern
Copy link

I just encountered the very same problem with this line 169.

A quick temporary fix was to replace the line 169 with :
{{ widget|default('')|raw }}

Google indicates me you are the only person having this problem as well, due to the fact we use this configuration :

    form:
        resources: ['bootstrap_3_layout.html.twig' ]

Did you find a way to fix this problem ?

@jasongabler
Copy link

Thank you so much for this example.

@fredericlam
Copy link

fredericlam commented Dec 8, 2017

Wow, awesome.

Thanks so much !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment