Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony Twig Form Theming of specific fields

I have a custom form field type and an associated form theme for it. On one page I have a lot of these fields, but one of the fields in particular I want to change.

Is there any way to theme certain fields of the same type (and in the same file) differently?

A simplified example:

form_fields.html.twig: (local theming file)

{% block my_dropdown_row %}
<div>
    {{ form_label(form) }}
    {{ form_widget(form) }}
    {{ form_errors(form) }}
</div>
{% endblock %}

In my form template (all these fields have the same type - my_dropdown

{{ form_row(form.selectionA) }}
{{ form_row(form.selectionB) }}
{{ form_row(form.selectionC) }}
{{ form_row(form.final_selection) }}

How can I style the final field differently to the others? There is a lot of code in these widgets so less duplication the better.

like image 693
lopsided Avatar asked Sep 06 '12 10:09

lopsided


2 Answers

This can be done. Here is how:

http://symfony.com/doc/current/cookbook/form/form_customization.html

like image 194
Carlos Granados Avatar answered Oct 23 '22 19:10

Carlos Granados


Old question and already answered, but would be nice to have more detailed info.

{% form_theme form _self %}

    {% block _product_name_widget %}
    <div class="text_widget">
        {{ block('form_widget_simple') }}
    </div>
{% endblock %}

{{ form_widget(form.name) }}

Where block name _product_name_widget stands for

_ form type(block prefix)_ form item name_render function

http://symfony.com/doc/current/form/form_customization.html#how-to-customize-an-individual-field

like image 20
Dmitri P Avatar answered Oct 23 '22 21:10

Dmitri P