Skip to content
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

fix(material/input): Do not set aria-invalid on emptymatInputs #22802

Merged

Conversation

ByzantineFailure
Copy link
Contributor

Updates the logic for setting aria-invalid on matInput to not set
the attribute at all if the input is required and has no value.

Prior to this PR matInput sets aria-invalid="false" for any empty
matInput, including required ones. This suppresses screen readers'
announcement to users that such inputs are in an invalid state.

Fixes #22777

@google-cla google-cla bot added the cla: yes PR author has agreed to Google's Contributor License Agreement label May 26, 2021
@ByzantineFailure
Copy link
Contributor Author

This is actually just a continuation of #22779 but I screwed up the branch pretty severely when I:
A) squash merged it (really not the root of the problem)
B) pushed the new branch name I had on the local since I messed up the name on my remote (the actual problem)

There is good discussion on that PR which needs to be followed up on. Copying over:

Via @crisbeto : This behavior is intentional, see #21609. There's also a linked issue.

Response:
Yep! The reason #21609 was requested was because without it aria-invalid was being applied to aria-required inputs with no value. This violates the ARIA spec (see here : https://www.w3.org/TR/wai-aria/#aria-invalid ) and caused #18140 to be filed.

Unfortunately, setting aria-invalid="false" on these inputs suppresses any native indication to a screen reader user that the input is in an invalid state. The only information they receive is the error message itself. If worded correctly the error message can be enough (e.g. if it includes "Invalid" in the text) but this requires component users to be aware of this fact and actively account for it.

Without a well-worded error message, this results in a different experience for SR users than visual users -- the input is flagged as invalid and displayed prominently to visual users as invalid, but screen reader users get no native notification at all when they focus the input.

@@ -1081,7 +1081,7 @@ describe('MatMdcInput with forms', () => {
expect(describedBy).toBe(errorIds);
}));

it('should not set `aria-invalid` to true if the input is empty', fakeAsync(() => {
it('should set `aria-invalid` to true if the input is empty', fakeAsync(() => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This test checks that a non-required input with a FormControl which contains Validators.required sets aria-invalid.

This actually is the edge case that @zelliott pointed out in #22779 when they mentioned that the logic for setting aria-invalid should actually be (empty && required) ? null : errorState.

In this case there is no required on the form field, and therefore no aria-required. This means we must set aria-invalid to indicate to a screen reader that there is some other validation error on the form field that they need to be aware of.

I'm not certain that this test's title is informative enough in this case -- thoughts?

@Component({
template: `
<mat-form-field>
<input matInput required [formControl]="formControl">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the ReactiveFormsModule discourages using required (and similar attributes) together with formControl, in favor of passing in Validator functions. That being said, we currently have an issue where we can't detect Validators.required from a FormControl because @angular/forms doesn't have an API for it. Are these tests capturing something that isn't captured by the other ngModel-based tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming I'm reading the tests correctly, what exists right now does a good job of testing a required matInput and a good job of testing a matInput which relies on the ReactiveFormsModule without the native required attribute.

These specifically test when both are present since the new logic considers both when assigning aria-required. If we are okay with leaving that scenario untested since it's discouraged by ReactiveFormsModule I'm fine to remove these tests. I included them for completeness' sake more than anything.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These ReactiveFormsModule tests can safely be removed since they don't reflect how the component should be used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests have been removed.

…Input`s

Updates the logic for setting `aria-invalid` on `matInput` to not set
the attribute at all if the input is `required` and has no value.

Prior to this PR `matInput` sets `aria-invalid="false"` for any empty
`matInput`, including `required` ones.  This suppresses screen readers'
announcement to users that such inputs are in an invalid state.

Fixes angular#22777
Copy link
Member

@crisbeto crisbeto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@crisbeto crisbeto added Accessibility This issue is related to accessibility (a11y) action: merge The PR is ready for merge by the caretaker P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent target: patch This PR is targeted for the next patch release labels Jun 2, 2021
@andrewseguin andrewseguin merged commit d185294 into angular:master Jun 7, 2021
andrewseguin pushed a commit that referenced this pull request Jun 7, 2021
…Input`s (#22802)

Updates the logic for setting `aria-invalid` on `matInput` to not set
the attribute at all if the input is `required` and has no value.

Prior to this PR `matInput` sets `aria-invalid="false"` for any empty
`matInput`, including `required` ones.  This suppresses screen readers'
announcement to users that such inputs are in an invalid state.

Fixes #22777

(cherry picked from commit d185294)
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Jul 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Accessibility This issue is related to accessibility (a11y) action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent target: patch This PR is targeted for the next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug(matInput): Screen Readers do not announce that an empty matInput[required] is invalid
3 participants