Skip to content

Commit bc2c2e4

Browse files
[TwigBridgeRessources] add aria-invalid and aria-describedby on form inputs when validation failure exist
1 parent 3f35b88 commit bc2c2e4

10 files changed

+48
-47
lines changed

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ col-sm-2
2222

2323
{# Rows #}
2424

25-
{% block form_row -%}
26-
{%- set widget_attr = {} -%}
27-
{%- if help -%}
28-
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
29-
{%- endif -%}
25+
{% block form_row_render -%}
3026
<div{% with {attr: row_attr|merge({class: (row_attr.class|default('') ~ ' form-group' ~ ((not compound or force_error|default(false)) and not valid ? ' has-error'))|trim})} %}{{ block('attributes') }}{% endwith %}>
3127
{{- form_label(form) -}}
3228
<div class="{{ block('form_group_class') }}">
@@ -35,7 +31,7 @@ col-sm-2
3531
{{- form_errors(form) -}}
3632
</div>
3733
{##}</div>
38-
{%- endblock form_row %}
34+
{%- endblock form_row_render -%}
3935

4036
{% block submit_row -%}
4137
<div{% with {attr: row_attr|merge({class: (row_attr.class|default('') ~ ' form-group')|trim})} %}{{ block('attributes') }}{% endwith %}>{#--#}

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,14 @@
127127

128128
{# Rows #}
129129

130-
{% block form_row -%}
131-
{%- set widget_attr = {} -%}
132-
{%- if help -%}
133-
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
134-
{%- endif -%}
130+
{% block form_row_render -%}
135131
<div{% with {attr: row_attr|merge({class: (row_attr.class|default('') ~ ' form-group' ~ ((not compound or force_error|default(false)) and not valid ? ' has-error'))|trim})} %}{{ block('attributes') }}{% endwith %}>
136132
{{- form_label(form) }} {# -#}
137133
{{ form_widget(form, widget_attr) }} {# -#}
138134
{{- form_help(form) -}}
139135
{{ form_errors(form) }} {# -#}
140136
</div> {# -#}
141-
{%- endblock form_row %}
137+
{%- endblock form_row_render -%}
142138

143139
{% block button_row -%}
144140
<div{% with {attr: row_attr|merge({class: (row_attr.class|default('') ~ ' form-group')|trim})} %}{{ block('attributes') }}{% endwith %}>
@@ -187,7 +183,7 @@
187183
{% block form_errors -%}
188184
{% if errors|length > 0 -%}
189185
{% if form is not rootform %}<span class="help-block">{% else %}<div class="alert alert-danger">{% endif %}
190-
<ul class="list-unstyled">
186+
<ul class="list-unstyled" id="{{ id ~ "_errors" }}">
191187
{%- for error in errors -%}
192188
<li><span class="glyphicon glyphicon-exclamation-sign"></span> {{ error.message }}</li>
193189
{%- endfor -%}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@ col-sm-2
2424
{%- if expanded is defined and expanded -%}
2525
{{ block('fieldset_form_row') }}
2626
{%- else -%}
27-
{%- set widget_attr = {} -%}
27+
{%- set attr = {} -%}
2828
{%- if help -%}
29-
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
29+
{%- set attr = attr|merge({'aria-describedby': id ~"_help"}) -%}
30+
{%- endif -%}
31+
{%- if errors|length > 0 -%}
32+
{%- set attr = attr|merge({'aria-invalid': 'true'}) -%}
3033
{%- endif -%}
34+
{%- set widget_attr = {attr: attr} -%}
3135
<div{% with {attr: row_attr|merge({class: (row_attr.class|default('') ~ ' form-group row' ~ ((not compound or force_error|default(false)) and not valid ? ' is-invalid'))|trim})} %}{{ block('attributes') }}{% endwith %}>
3236
{{- form_label(form) -}}
3337
<div class="{{ block('form_group_class') }}">

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,14 @@
282282
{%- if compound is defined and compound -%}
283283
{%- set element = 'fieldset' -%}
284284
{%- endif -%}
285-
{%- set widget_attr = {} -%}
285+
{%- set attr = {} -%}
286286
{%- if help -%}
287-
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
287+
{%- set attr = attr|merge({'aria-describedby': id ~"_help"}) -%}
288288
{%- endif -%}
289+
{%- if errors|length > 0 -%}
290+
{%- set attr = attr|merge({'aria-invalid': 'true'}) -%}
291+
{%- endif -%}
292+
{%- set widget_attr = {attr: attr} -%}
289293
<{{ element|default('div') }}{% with {attr: row_attr|merge({class: (row_attr.class|default('') ~ ' form-group')|trim})} %}{{ block('attributes') }}{% endwith %}>
290294
{{- form_label(form) -}}
291295
{{- form_widget(form, widget_attr) -}}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,10 @@
2323

2424
{# Rows #}
2525

26-
{% block form_row -%}
26+
{% block form_row_render -%}
2727
{%- if expanded is defined and expanded -%}
2828
{{ block('fieldset_form_row') }}
2929
{%- else -%}
30-
{%- set widget_attr = {} -%}
31-
{%- if help -%}
32-
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
33-
{%- endif -%}
3430
{%- set row_class = row_class|default(row_attr.class|default('mb-3')) -%}
3531
{%- set is_form_floating = is_form_floating|default('form-floating' in row_class) -%}
3632
{%- set is_input_group = is_input_group|default('input-group' in row_class) -%}
@@ -68,7 +64,7 @@
6864
{%- endif -%}
6965
{##}</div>
7066
{%- endif -%}
71-
{%- endblock form_row %}
67+
{%- endblock form_row_render %}
7268

7369
{% block fieldset_form_row -%}
7470
{%- set widget_attr = {} -%}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -320,14 +320,10 @@
320320

321321
{# Rows #}
322322

323-
{%- block form_row -%}
323+
{%- block form_row_render -%}
324324
{%- if compound is defined and compound -%}
325325
{%- set element = 'fieldset' -%}
326326
{%- endif -%}
327-
{%- set widget_attr = {} -%}
328-
{%- if help -%}
329-
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
330-
{%- endif -%}
331327
{%- set row_class = row_class|default(row_attr.class|default('mb-3')|trim) -%}
332328
<{{ element|default('div') }}{% with {attr: row_attr|merge({class: row_class})} %}{{ block('attributes') }}{% endwith %}>
333329
{%- if 'form-floating' in row_class -%}
@@ -340,7 +336,7 @@
340336
{{- form_help(form) -}}
341337
{{- form_errors(form) -}}
342338
</{{ element|default('div') }}>
343-
{%- endblock form_row %}
339+
{%- endblock form_row_render %}
344340

345341
{%- block button_row -%}
346342
<div{% with {attr: row_attr|merge({class: row_attr.class|default('mb-3')|trim})} %}{{ block('attributes') }}{% endwith %}>
@@ -352,9 +348,11 @@
352348

353349
{%- block form_errors -%}
354350
{%- if errors|length > 0 -%}
351+
<div id="{{ id ~ "_errors" }}">
355352
{%- for error in errors -%}
356353
<div class="{% if form is not rootform %}invalid-feedback{% else %}alert alert-danger{% endif %} d-block">{{ error.message }}</div>
357354
{%- endfor -%}
355+
</div>
358356
{%- endif %}
359357
{%- endblock form_errors %}
360358

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,17 +366,32 @@
366366
{%- endblock repeated_row -%}
367367

368368
{%- block form_row -%}
369-
{%- set widget_attr = {} -%}
369+
{%- set attr = {} -%}
370+
{%- set aria_describedby = [] -%}
370371
{%- if help -%}
371-
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
372+
{%- set aria_describedby = aria_describedby|merge([id ~ "_help"]) -%}
372373
{%- endif -%}
374+
{%- if errors|length > 0 -%}
375+
{%- set aria_describedby = aria_describedby|merge([id ~ "_errors"]) -%}
376+
{%- endif -%}
377+
{%- if aria_describedby|length > 0 -%}
378+
{%- set attr = attr|merge({'aria-describedby': aria_describedby|join(' ')}) -%}
379+
{%- endif -%}
380+
{%- if errors|length > 0 -%}
381+
{%- set attr = attr|merge({'aria-invalid': 'true'}) -%}
382+
{%- endif -%}
383+
{%- set widget_attr = {attr: attr} -%}
384+
{{- block('form_row_render') -}}
385+
{%- endblock form_row -%}
386+
387+
{%- block form_row_render -%}
373388
<div{% with {attr: row_attr} %}{{ block('attributes') }}{% endwith %}>
374389
{{- form_label(form) -}}
375390
{{- form_errors(form) -}}
376391
{{- form_widget(form, widget_attr) -}}
377392
{{- form_help(form) -}}
378393
</div>
379-
{%- endblock form_row -%}
394+
{%- endblock form_row_render -%}
380395

381396
{%- block button_row -%}
382397
<div{% with {attr: row_attr} %}{{ block('attributes') }}{% endwith %}>
@@ -419,7 +434,7 @@
419434

420435
{%- block form_errors -%}
421436
{%- if errors|length > 0 -%}
422-
<ul>
437+
<ul id="{{ id ~ "_errors" }}">
423438
{%- for error in errors -%}
424439
<li>{{ error.message }}</li>
425440
{%- endfor -%}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
{% use "form_div_layout.html.twig" %}
22

3-
{%- block form_row -%}
4-
{%- set widget_attr = {} -%}
5-
{%- if help -%}
6-
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
7-
{%- endif -%}
3+
{%- block form_row_render -%}
84
<tr{% with {attr: row_attr} %}{{ block('attributes') }}{% endwith %}>
95
<td>
106
{{- form_label(form) -}}
@@ -15,7 +11,7 @@
1511
{{- form_help(form) -}}
1612
</td>
1713
</tr>
18-
{%- endblock form_row -%}
14+
{%- endblock form_row_render -%}
1915

2016
{%- block button_row -%}
2117
<tr{% with {attr: row_attr} %}{{ block('attributes') }}{% endwith %}>

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,7 @@
281281

282282
{# Rows #}
283283

284-
{% block form_row -%}
285-
{%- set widget_attr = {} -%}
286-
{%- if help -%}
287-
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
288-
{%- endif -%}
284+
{% block form_row_render -%}
289285
<div{% with {attr: row_attr|merge({class: (row_attr.class|default('') ~ ' row')|trim})} %}{{ block('attributes') }}{% endwith %}>
290286
<div class="large-12 columns{% if (not compound or force_error|default(false)) and not valid %} error{% endif %}">
291287
{{- form_label(form) -}}
@@ -294,7 +290,7 @@
294290
{{- form_errors(form) -}}
295291
</div>
296292
</div>
297-
{%- endblock form_row %}
293+
{%- endblock form_row_render %}
298294

299295
{% block choice_row -%}
300296
{% set force_error = true %}
@@ -340,7 +336,7 @@
340336

341337
{% block form_errors -%}
342338
{% if errors|length > 0 -%}
343-
{% if form is not rootform %}<small class="error">{% else %}<div data-alert class="alert-box alert">{% endif %}
339+
{% if form is not rootform %}<small class="error" id="{{ id ~ "_errors" }}">{% else %}<div data-alert class="alert-box alert" id="{{ id ~ "_errors" }}">{% endif %}
344340
{%- for error in errors -%}
345341
{{ error.message }}
346342
{% if not loop.last %}, {% endif %}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy