Skip to content

Commit d5cafa2

Browse files
apply individual error ID logic to all templates for better maintainability
1 parent 929a61a commit d5cafa2

File tree

7 files changed

+13
-33
lines changed

7 files changed

+13
-33
lines changed

src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_3_layout.html.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@
183183
{% block form_errors -%}
184184
{% if errors|length > 0 -%}
185185
{% if form is not rootform %}<span class="help-block">{% else %}<div class="alert alert-danger">{% endif %}
186-
<ul class="list-unstyled" id="{{ id ~ "_errors" }}">
186+
<ul class="list-unstyled">
187187
{%- for error in errors -%}
188-
<li><span class="glyphicon glyphicon-exclamation-sign"></span> {{ error.message }}</li>
188+
<li id="{{ id ~ '_error' ~ loop.index }}"><span class="glyphicon glyphicon-exclamation-sign"></span> {{ error.message }}</li>
189189
{%- endfor -%}
190190
</ul>
191191
{% if form is not rootform %}</span>{% else %}</div>{% endif %}

src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_4_horizontal_layout.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ col-sm-2
2626
{%- else -%}
2727
{%- set attr = {} -%}
2828
{%- if help -%}
29-
{%- set attr = attr|merge({'aria-describedby': id ~"_help"}) -%}
29+
{%- set attr = attr|merge({'aria-describedby': id ~ '_help'}) -%}
3030
{%- endif -%}
3131
{%- if errors|length > 0 -%}
3232
{%- set attr = attr|merge({'aria-invalid': 'true'}) -%}

src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_4_layout.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@
284284
{%- endif -%}
285285
{%- set attr = {} -%}
286286
{%- if help -%}
287-
{%- set attr = attr|merge({'aria-describedby': id ~'_help'}) -%}
287+
{%- set attr = attr|merge({'aria-describedby': id ~ '_help'}) -%}
288288
{%- endif -%}
289289
{%- if errors|length > 0 -%}
290290
{%- set attr = attr|merge({'aria-invalid': 'true'}) -%}

src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_5_layout.html.twig

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -320,25 +320,6 @@
320320

321321
{# Rows #}
322322

323-
{%- block form_row -%}
324-
{%- set attr = {} -%}
325-
{%- set aria_describedby = [] -%}
326-
{%- if help -%}
327-
{%- set aria_describedby = aria_describedby|merge([id ~ "_help"]) -%}
328-
{%- endif -%}
329-
{%- if errors|length > 0 -%}
330-
{%- set aria_describedby = aria_describedby|merge(errors|map((error, index) => id ~ "_error" ~ (index+1))) -%}
331-
{%- endif -%}
332-
{%- if aria_describedby|length > 0 -%}
333-
{%- set attr = attr|merge({'aria-describedby': aria_describedby|join(' ')}) -%}
334-
{%- endif -%}
335-
{%- if errors|length > 0 -%}
336-
{%- set attr = attr|merge({'aria-invalid': 'true'}) -%}
337-
{%- endif -%}
338-
{%- set widget_attr = {attr: attr} -%}
339-
{{- block('form_row_render') -}}
340-
{%- endblock form_row -%}
341-
342323
{%- block form_row_render -%}
343324
{%- if compound is defined and compound -%}
344325
{%- set element = 'fieldset' -%}
@@ -368,7 +349,7 @@
368349
{%- block form_errors -%}
369350
{%- if errors|length > 0 -%}
370351
{%- for error in errors -%}
371-
<div class="{% if form is not rootform %}invalid-feedback{% else %}alert alert-danger{% endif %} d-block" id="{{ id ~ "_error" ~ loop.index }}">{{ error.message }}</div>
352+
<div class="{% if form is not rootform %}invalid-feedback{% else %}alert alert-danger{% endif %} d-block" id="{{ id ~ '_error' ~ loop.index }}">{{ error.message }}</div>
372353
{%- endfor -%}
373354
{%- endif %}
374355
{%- endblock form_errors %}

src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,10 @@
347347
{%- set attr = {} -%}
348348
{%- set aria_describedby = [] -%}
349349
{%- if help -%}
350-
{%- set aria_describedby = aria_describedby|merge([id ~ "_help"]) -%}
350+
{%- set aria_describedby = aria_describedby|merge([id ~ '_help']) -%}
351351
{%- endif -%}
352352
{%- if errors|length > 0 -%}
353-
{%- set aria_describedby = aria_describedby|merge([id ~ "_errors"]) -%}
353+
{%- set aria_describedby = aria_describedby|merge(errors|map((_, index) => id ~ '_error' ~ (index + 1))) -%}
354354
{%- endif -%}
355355
{%- if aria_describedby|length > 0 -%}
356356
{%- set attr = attr|merge({'aria-describedby': aria_describedby|join(' ')}) -%}
@@ -412,9 +412,9 @@
412412

413413
{%- block form_errors -%}
414414
{%- if errors|length > 0 -%}
415-
<ul id="{{ id ~ "_errors" }}">
415+
<ul>
416416
{%- for error in errors -%}
417-
<li>{{ error.message }}</li>
417+
<li id="{{ id ~ '_error' ~ loop.index }}">{{ error.message }}</li>
418418
{%- endfor -%}
419419
</ul>
420420
{%- endif -%}

src/Symfony/Bridge/Twig/Resources/views/Form/foundation_5_layout.html.twig

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,9 @@
336336

337337
{% block form_errors -%}
338338
{% if errors|length > 0 -%}
339-
{% if form is not rootform %}<small class="error" id="{{ id ~ "_errors" }}">{% else %}<div data-alert class="alert-box alert" id="{{ id ~ "_errors" }}">{% endif %}
339+
{% if form is not rootform %}<small class="error">{% else %}<div data-alert class="alert-box alert">{% endif %}
340340
{%- for error in errors -%}
341-
{{ error.message }}
342-
{% if not loop.last %}, {% endif %}
341+
<span id="{{ id ~ '_error' ~ loop.index }}">{{ error.message }}</span>{% if not loop.last %}, {% endif %}
343342
{%- endfor -%}
344343
{% if form is not rootform %}</small>{% else %}</div>{% endif %}
345344
{%- endif %}

src/Symfony/Bridge/Twig/Resources/views/Form/tailwind_2_layout.html.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222

2323
{%- block form_errors -%}
2424
{%- if errors|length > 0 -%}
25-
<ul id="{{ id ~ "_errors" }}">
25+
<ul>
2626
{%- for error in errors -%}
27-
<li class="{{ error_item_class|default('text-red-700') }}">{{ error.message }}</li>
27+
<li class="{{ error_item_class|default('text-red-700') }}" id="{{ id ~ '_error' ~ loop.index }}">{{ error.message }}</li>
2828
{%- endfor -%}
2929
</ul>
3030
{%- endif -%}

0 commit comments

Comments
 (0)