Skip to content

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

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/material-experimental/mdc-input/input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?

// Submit the form since it's the one that triggers the default error state matcher.
dispatchFakeEvent(fixture.nativeElement.querySelector('form'), 'submit');
fixture.detectChanges();
Expand All @@ -1090,7 +1090,7 @@ describe('MatMdcInput with forms', () => {
expect(testComponent.formControl.invalid).toBe(true, 'Expected form control to be invalid');
expect(inputEl.value).toBeFalsy();
expect(inputEl.getAttribute('aria-invalid'))
.toBe('false', 'Expected aria-invalid to be set to "false".');
.toBe('true', 'Expected aria-invalid to be set to "true"');

inputEl.value = 'not valid';
fixture.detectChanges();
Expand Down
2 changes: 1 addition & 1 deletion src/material-experimental/mdc-input/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {MatInput as BaseMatInput} from '@angular/material/input';
'[attr.readonly]': 'readonly && !_isNativeSelect || null',
// Only mark the input as invalid for assistive technology if it has a value since the
// state usually overlaps with `aria-required` when the input is empty and can be redundant.
'[attr.aria-invalid]': 'errorState && !empty',
'[attr.aria-invalid]': '(empty && required) ? null : errorState',
'[attr.aria-required]': 'required',
},
providers: [{provide: MatFormFieldControl, useExisting: MatInput}],
Expand Down
4 changes: 2 additions & 2 deletions src/material/input/input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1219,7 +1219,7 @@ describe('MatInput 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(() => {
// Submit the form since it's the one that triggers the default error state matcher.
dispatchFakeEvent(fixture.nativeElement.querySelector('form'), 'submit');
fixture.detectChanges();
Expand All @@ -1228,7 +1228,7 @@ describe('MatInput with forms', () => {
expect(testComponent.formControl.invalid).toBe(true, 'Expected form control to be invalid');
expect(inputEl.value).toBeFalsy();
expect(inputEl.getAttribute('aria-invalid'))
.toBe('false', 'Expected aria-invalid to be set to "false".');
.toBe('true', 'Expected aria-invalid to be set to "true".');

inputEl.value = 'not valid';
fixture.detectChanges();
Expand Down
2 changes: 1 addition & 1 deletion src/material/input/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const _MatInputBase = mixinErrorState(class {
'[attr.readonly]': 'readonly && !_isNativeSelect || null',
// Only mark the input as invalid for assistive technology if it has a value since the
// state usually overlaps with `aria-required` when the input is empty and can be redundant.
'[attr.aria-invalid]': 'errorState && !empty',
'[attr.aria-invalid]': '(empty && required) ? null : errorState',
'[attr.aria-required]': 'required',
},
providers: [{provide: MatFormFieldControl, useExisting: MatInput}],
Expand Down