Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize form_label block in form template in Symfony2?

I'm trying to customize form_label in a template which already extends one template.

I'm using 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 change!

Can you help me?

like image 620
Lucas M. Avatar asked Mar 26 '13 18:03

Lucas M.


1 Answers

Here is my solution.

In top of my form.html.twig file:

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

and now into fields.html.twig, I custom the 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 %}
like image 87
Lucas M. Avatar answered Nov 15 '22 08:11

Lucas M.