jeudi 23 juillet 2015

Twig Form - Don't show label for checkbox

This might be simple but I have a label and checkbox overriden blocks in my form theme (among many others). I want all label to be handled by the form_label block except for checkboxs'.

I am currently wrapping the label rendering in an if statement which I don't want to do as it doesn't "feel clean":

{%-  if form.vars.block_prefixes.1 is not defined or form.vars.block_prefixes.1 != 'checkbox' -%}

Here is are my overridden blocks, any chance I can disable the label in the checkbox_widget block?

{#
    ############# Checkbox #############
#}

{%- block checkbox_widget -%}
    {% spaceless %}
        <label  for="{{ id }}"><input type="checkbox" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} />{{ label|raw }}</label>
    {% endspaceless %}
{%- endblock checkbox_widget -%}

{#
############# Labels #############
#}

{%- block form_label -%}
    {% if label is not sameas(false) -%}
        {% set label_attr = label_attr|merge({'class': ('FormItem-label' ~ label_attr.class|default(''))|trim}) %}
        {% 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('') ~ ' is-required')|trim}) %}
        {%- endif %}
        {% if label is empty -%}
            {%- if label_format is not empty -%}
                {% set label = label_format|replace({
                    '%name%': name,
                    '%id%': id,
                }) %}
            {%- else -%}
                {% set label = name|humanize %}
            {%- endif -%}
        {%- endif -%}
        {%-  if form.vars.block_prefixes.1 is not defined or form.vars.block_prefixes.1 != 'checkbox' -%}
            <label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ label|trans({}, translation_domain) }}</label>
        {%- endif -%}
    {%- endif -%}
{%- endblock form_label -%}

{%- block button_label -%}{%- endblock -%}




Aucun commentaire:

Enregistrer un commentaire