How to customize form_label block in form template in symfony2?

I am trying to configure form_label in a template that already extends one template.

I use the example in the Symfony2 documentation :

{% use 'form_div_layout.html.twig' with form_label as base_form_label %}

{% block form_label %}
    {{ block('base_form_label') }}

    {% if required %}
        <span class="required" title="This field is required">*</span>
    {% endif %}
{% endblock %}

but nothing changes!

Can you help me?

+5
source share
1 answer

Here is my solution.

At the top of my form.html.twig file:

{% form_theme form with 'MyBundle:Activity:Form/fields.html.twig' %}

and now in fields.html.twig, I'm setting up form_label:

{% extends 'form_div_layout.html.twig' %}

{% block form_label %}
{% spaceless %}
    {% if not compound %}
        {% set label_attr = label_attr|merge({'for': id}) %}
    {% endif %}
    {% if required %}
        {% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
    {% endif %}
    {% if label is empty %}
        {% set label = name|humanize %}
    {% endif %}

    <label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ label|trans({}, translation_domain) }}{% if attr.note is defined %} <span style="font: 11px normal; font-family: arial;">({{ attr.note }})</span>{% endif %}</label>
{% endspaceless %}
{% endblock form_label %}
+7
source

All Articles