Created
October 17, 2011 23:32
-
-
Save cowlby/1294186 to your computer and use it in GitHub Desktop.
Customizing form collections in Symfony2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% 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 %} |
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 ?
Thank you so much for this example.
Wow, awesome.
Thanks so much !
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 %}