0
votes

I have a project in Symfony2 where I have lots of different forms that do lots of different things and one thing I am struggling with at the moment is working out how I can set two different templates to handle form_rows.

I have specified one form_row template in a fiels.html.twig file that handles form rows as follows:

{% block form_row %}
    {% spaceless %}
        <div class="form-element{% if errors %} form-element-error{% endif %} widget-group clearfix">
            <div class="widget widget-1-of-3">
                {{ form_label(form) }}
            </div>
            <div class="widget widget-2-of-3">
                {{ form_widget(form) }}
                {% if errors %}
                    <small>{{ form_errors(form) }}</small>
                {% else %}
                    {% if help is defined %}
                        <small>{{ help }}</small>
                    {% endif %}
                {% endif %}
            </div>
        </div>
    {% endspaceless %}
{% endblock form_row %}

This works great for me for general forms, but I also have a number of form elements that I have in tables as well. This is generally for groups of form data that I am updating.

In this case I then want to be able to specify using a different form_rows template.

I understand that I could specify the row via the field name or group, but I have a lot and I just want a more general way of doing this.

Does anyone know how this could be achieved.

1

1 Answers

2
votes

You can have a lot of form themes defined in different templates. Just add in your twig file on the top where you want to use theme:

{% form_theme form 'AcmeDemoBundle:Form:fields.html.twig' %}

{{ form_widget(form) }}

where form_theme file can be any twig file with theme definition. Now form widget is decorated with attached theme.