Reputation: 1587
I am trying to customize Symfony form label to add an asterisk(*) for all required fields through this Symfony doc. But my asterisk <span
has to be inside the <label
tag so I had to customize form_label
block as described here. So far is good, but the customization also being applied to each item(label) of checkbox/radio fields. where it looks odd.
Any idea, how could I filter it in label customized block to format only for parent labels.
Here is my overridden code :
{% block form_label -%}
{% if label is not sameas(false) -%}
{% 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 required %} <span class="required" title="This field is required">*</span> {% endif %}
</label>
{%- endif %}
{%- endblock form_label %}
In brief, I want a variable inside this block to identify the field type this label is targeting to.
Upvotes: 0
Views: 502
Reputation: 1587
Well, I resolved this with some tweak from the variable available in this overridden block. Below is my overridden code for condition
{% if (required) and ( form.vars.checked is not defined ) %}
<span class="validation-error-star" title="This field is required">*</span>
{% endif %}
Instead of only required variable as condition, I further added another condition if there is any checked attribute defined for this field; which normally a radio/checkbox field type has.
This helped me to differentiate the field type inside a label overridden block. hope this helps somebody. :)
Upvotes: 1