-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[TwigBridgeRessources] add aria-invalid and aria-describedby on form inputs when validation failure exist #60902
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TwigBridgeRessources] add aria-invalid and aria-describedby on form inputs when validation failure exist #60902
Conversation
|
Hey! I see that this is your first PR. That is great! Welcome! Symfony has a contribution guide which I suggest you to read. In short:
Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change. When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor! I am going to sit back now and wait for the reviews. Cheers! Carsonbot |
nicolas-grekas
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks nice to me. Can you please add a line in the changelog of the bridge, section 7.4?
|
Hi, thanks for your feedback :) |
| {% if errors|length > 0 -%} | ||
| {% if form is not rootform %}<span class="help-block">{% else %}<div class="alert alert-danger">{% endif %} | ||
| <ul class="list-unstyled"> | ||
| <ul class="list-unstyled" id="{{ id ~ "_errors" }}"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm pretty sure this isn't covered by any BC policy, but... it's been considered a best practice for years in the Symfony / front-end aware community. I'm just a bit concerned this might lead to duplicate ids on the page.
So I’d recommend we communicate clearly and visibly about this (very welcome!) change—just to avoid penalizing those who were being cautious.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here :)
| {%- set attr = {} -%} | ||
| {%- if help is not empty -%} | ||
| {%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%} | ||
| {%- set attr = attr|merge({'aria-describedby': id ~"_help"}) -%} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(same here)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here too
|
A big 👍 for the direction — we just need to be careful here, or we might end up hurting the very people who were already sensitive to this topic. |
|
Hi @smnandre, you left a review on this PR 2 weeks ago, and because there was conflicts on it since last changes on branch 7.4, I wanted to rebase it. But I think I've made a (big) mistake 🫣. So sorry, your comments are gone, and instead of 11 files changed, there's now 678 files. This is my first contrib to the framework. |
|
TL;DR; these changes will break a lot of form integrations... I need to see once you have rebased You did rename form blocks, right ? Or moved some attributes from place ? If "yes" this will break a lot of websites ... :| |
|
Thanks for your quick feedback. |
|
And to be very precise, the rebase is done and I don't understand why I got this result of 678 files. |
b8d1c4c to
d9ca05c
Compare
|
@smnandre please forget my last message, rebase is done with a clean history now I think. Sorry for my confusion. |
d9ca05c to
ed88b5c
Compare
|
Arf, tests fail because of this. Can you please have a look? Maybe this patch helps?diff --git a/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_5_horizontal_layout.html.twig b/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_5_horizontal_layout.html.twig
index 2086c683ed2..fff657c37db 100644
--- a/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_5_horizontal_layout.html.twig
+++ b/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_5_horizontal_layout.html.twig
@@ -23,6 +23,25 @@
{# Rows #}
+{% block form_row -%}
+ {%- set attr = {} -%}
+ {%- set aria_describedby = [] -%}
+ {%- if help -%}
+ {%- set aria_describedby = aria_describedby|merge([id ~ "_help"]) -%}
+ {%- endif -%}
+ {%- if errors|length > 0 -%}
+ {%- set aria_describedby = aria_describedby|merge([id ~ "_errors"]) -%}
+ {%- endif -%}
+ {%- if aria_describedby|length > 0 -%}
+ {%- set attr = attr|merge({'aria-describedby': aria_describedby|join(' ')}) -%}
+ {%- endif -%}
+ {%- if errors|length > 0 -%}
+ {%- set attr = attr|merge({'aria-invalid': 'true'}) -%}
+ {%- endif -%}
+ {%- set widget_attr = {attr: attr} -%}
+ {{- block('form_row_render') -}}
+{%- endblock form_row %}
+
{% block form_row_render -%}
{%- if expanded is defined and expanded -%}
{{ block('fieldset_form_row') }}
diff --git a/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_5_layout.html.twig b/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_5_layout.html.twig
index 5e5abc03c15..32d006b01ea 100644
--- a/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_5_layout.html.twig
+++ b/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_5_layout.html.twig
@@ -320,6 +320,25 @@
{# Rows #}
+{%- block form_row -%}
+ {%- set attr = {} -%}
+ {%- set aria_describedby = [] -%}
+ {%- if help -%}
+ {%- set aria_describedby = aria_describedby|merge([id ~ "_help"]) -%}
+ {%- endif -%}
+ {%- if errors|length > 0 -%}
+ {%- set aria_describedby = aria_describedby|merge([id ~ "_errors"]) -%}
+ {%- endif -%}
+ {%- if aria_describedby|length > 0 -%}
+ {%- set attr = attr|merge({'aria-describedby': aria_describedby|join(' ')}) -%}
+ {%- endif -%}
+ {%- if errors|length > 0 -%}
+ {%- set attr = attr|merge({'aria-invalid': 'true'}) -%}
+ {%- endif -%}
+ {%- set widget_attr = {attr: attr} -%}
+ {{- block('form_row_render') -}}
+{%- endblock form_row %}
+
{%- block form_row_render -%}
{%- if compound is defined and compound -%}
{%- set element = 'fieldset' -%}
diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap5HorizontalLayoutTestCase.php b/src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap5HorizontalLayoutTestCase.php
index 1c02f9e1020..7fdafec95b2 100644
--- a/src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap5HorizontalLayoutTestCase.php
+++ b/src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap5HorizontalLayoutTestCase.php
@@ -43,9 +43,11 @@ abstract class AbstractBootstrap5HorizontalLayoutTestCase extends AbstractBootst
[@class="col-sm-10"]
[
./input[@id="name"]
- /following-sibling::div
- [@class="invalid-feedback d-block"]
- [.="[trans]Error![/trans]"]
+ /following-sibling::div[@id="name_errors"]
+ [
+ ./div[@class="invalid-feedback d-block"]
+ [.="[trans]Error![/trans]"]
+ ]
]
[count(./div)=1]
]
@@ -74,9 +76,11 @@ abstract class AbstractBootstrap5HorizontalLayoutTestCase extends AbstractBootst
[@class="col-sm-10"]
[
./input[@id="name"]
- /following-sibling::div
- [@class="invalid-feedback d-block"]
- [.="[trans]Error![/trans]"]
+ /following-sibling::div[@id="name_errors"]
+ [
+ ./div[@class="invalid-feedback d-block"]
+ [.="[trans]Error![/trans]"]
+ ]
]
[count(./div)=1]
]
diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap5LayoutTestCase.php b/src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap5LayoutTestCase.php
index 4b3aa7fa5eb..780beaddc56 100644
--- a/src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap5LayoutTestCase.php
+++ b/src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap5LayoutTestCase.php
@@ -50,9 +50,11 @@ abstract class AbstractBootstrap5LayoutTestCase extends AbstractBootstrap4Layout
[
./label[@for="name"]
/following-sibling::input[@id="name"]
- /following-sibling::div
- [@class="invalid-feedback d-block"]
- [.="[trans]Error![/trans]"]
+ /following-sibling::div[@id="name_errors"]
+ [
+ ./div[@class="invalid-feedback d-block"]
+ [.="[trans]Error![/trans]"]
+ ]
]
[count(./div)=1]
'
@@ -75,9 +77,11 @@ abstract class AbstractBootstrap5LayoutTestCase extends AbstractBootstrap4Layout
[
./label[@for="name"]
/following-sibling::input[@id="name"]
- /following-sibling::div
- [@class="invalid-feedback d-block"]
- [.="[trans]Error![/trans]"]
+ /following-sibling::div[@id="name_errors"]
+ [
+ ./div[@class="invalid-feedback d-block"]
+ [.="[trans]Error![/trans]"]
+ ]
]
[count(./div)=1]
'
@@ -320,12 +324,13 @@ abstract class AbstractBootstrap5LayoutTestCase extends AbstractBootstrap4Layout
$html = $this->renderErrors($form->createView());
$this->assertMatchesXpath($html,
- '/div
- [@class="alert alert-danger d-block"]
- [.="[trans]Error 1[/trans]"]
- /following-sibling::div
- [@class="alert alert-danger d-block"]
- [.="[trans]Error 2[/trans]"]
+ '/div[@id="_errors"]
+ [
+ ./div[@class="alert alert-danger d-block"]
+ [.="[trans]Error 1[/trans]"]
+ /following-sibling::div[@class="alert alert-danger d-block"]
+ [.="[trans]Error 2[/trans]"]
+ ]
'
);
}
@@ -338,12 +343,13 @@ abstract class AbstractBootstrap5LayoutTestCase extends AbstractBootstrap4Layout
$html = $this->renderErrors($form->get('name')->createView());
$this->assertMatchesXpath($html,
- '/div
- [@class="invalid-feedback d-block"]
- [.="[trans]Error 1[/trans]"]
- /following-sibling::div
- [@class="invalid-feedback d-block"]
- [.="[trans]Error 2[/trans]"]
+ '/div[@id="name_errors"]
+ [
+ ./div[@class="invalid-feedback d-block"]
+ [.="[trans]Error 1[/trans]"]
+ /following-sibling::div[@class="invalid-feedback d-block"]
+ [.="[trans]Error 2[/trans]"]
+ ]
'
);
} |
4df56ab to
d5cafa2
Compare
|
Hi, |
…inputs when validation failure exist
d5cafa2 to
6957eba
Compare
|
Thank you @jeanfrancois-morin. |
Context and objective
This PR aims to add the appropriate ARIA attributes (
aria-invalidandaria-describedby) to form fields when validation errors are present.The proposal is based on the recommendations of WCAG 2.1 and specifically the technique ARIA21, which describes the combined use of
aria-invalid="true"andaria-describedby="<TEXT_ERROR_ID>"AFTER user input validation. It seems particularly relevant to implement this since SymfonyUX's LiveComponent allows for an auto-validation system, enabling real-time error feedback to users as they fill out the form.Concretely, users of screen readers will be informed of the presence of errors as soon as they focus on the form field, and if an explicit error message is associated, they can access it immediately.
Note that the
aria-describedbyattribute can point to one or more HTML elements, allowing multiple messages (for example, a help message and an error message) to be vocalized. This PR takes this possibility into account by associating each field with the text passages_field_id_help_and_field_id_errors_when applicable.Twig Side
The proposal is integrated into all form themes through the main template
form_div_layout.html.twig, except for the filesbootstrap_4_layout.html.twigandbootstrap_4_horizontal_layout.html.twig. In these files, errors were initially placed inside the associated label, so it is not necessary to add thearia-describedbyattribute. Errors will be vocalized automatically and immediately after the label content. Thearia-invalid="true"attribute remains necessary to indicate the presence of errors.Let me know if this approach works for you, or if you would like me to modify any aspects.