fix(select): improve screen reader announcement timing for validation errors (#30723)

Issue number: internal

---------

<!-- Please do not submit updates to dependencies unless it fixes an
issue. -->

<!-- Please try to limit your pull request to one type (bugfix, feature,
etc). Submit multiple pull requests if needed. -->

## What is the current behavior?
<!-- Please describe the current behavior that you are modifying. -->

Currently, when an error text is shown, it may not announce itself to
voice assistants. This is because the way error text currently works is
by always existing in the DOM, but being hidden when there is no error.
When the error state changes, the error text is shown, but as far as the
voice assistant can tell it's always been there and nothing has changed.

## What is the new behavior?
<!-- Please describe the behavior or changes that are being added by
this PR. -->

- Updated aria attributes
- Added observer with an observer

We had to do this with a mutation observer and state because it's
important in some frameworks, like Angular, that state changes to cause
a re-render. This, combined with some minor aria changes, makes it so
that when a field is declared invalid, it immediately announces the
invalid state instead of waiting for the user to go back to the invalid
field.

## Does this introduce a breaking change?

- [ ] Yes
- [x] No

<!--
  If this introduces a breaking change:
1. Describe the impact and migration path for existing applications
below.
  2. Update the BREAKING.md file with the breaking change.
3. Add "BREAKING CHANGE: [...]" to the commit description when merging.
See
https://github.com/ionic-team/ionic-framework/blob/main/docs/CONTRIBUTING.md#footer
for more information.
-->


## Other information

<!-- Any other information that is important to this PR such as
screenshots of how the component looks before and after the change. -->


[Preview](https://ionic-framework-git-fw-6797-ionic1.vercel.app/src/components/select/test/validation/)
This commit is contained in:
Maria Hutt
2025-10-15 10:50:07 -07:00
committed by GitHub
parent 18e1d3e1b8
commit 03303d73f0
13 changed files with 482 additions and 37 deletions

View File

@ -77,6 +77,31 @@
<p>MinLength Errors: <span id="minlength-errors">{{minLengthField.errors | json}}</span></p>
</ion-label>
</ion-item>
<!-- Test ion-select with required validation -->
<ion-item>
<ion-select
label="Required Select"
[(ngModel)]="selectValue"
name="selectField"
required
#selectField="ngModel"
id="template-select-test"
errorText="This field is required"
helperText="Select an option">
<ion-select-option value="option1">Option 1</ion-select-option>
<ion-select-option value="option2">Option 2</ion-select-option>
</ion-select>
</ion-item>
<!-- Display validation state for debugging -->
<ion-item>
<ion-label>
<p>Select Touched: <span id="select-touched">{{selectField.touched}}</span></p>
<p>Select Invalid: <span id="select-invalid">{{selectField.invalid}}</span></p>
<p>Select Errors: <span id="select-errors">{{selectField.errors | json}}</span></p>
</ion-label>
</ion-item>
</ion-list>
<div class="ion-padding">

View File

@ -9,6 +9,7 @@ export class TemplateFormComponent {
inputValue = '';
textareaValue = '';
minLengthValue = '';
selectValue = '';
// Track if form has been submitted
submitted = false;

View File

@ -47,6 +47,7 @@ export const routes: Routes = [
children: [
{ path: 'input-validation', loadComponent: () => import('../validation/input-validation/input-validation.component').then(c => c.InputValidationComponent) },
{ path: 'textarea-validation', loadComponent: () => import('../validation/textarea-validation/textarea-validation.component').then(c => c.TextareaValidationComponent) },
{ path: 'select-validation', loadComponent: () => import('../validation/select-validation/select-validation.component').then(c => c.SelectValidationComponent) },
{ path: '**', redirectTo: 'input-validation' }
]
},

View File

@ -131,6 +131,11 @@
Textarea Validation Test
</ion-label>
</ion-item>
<ion-item routerLink="/standalone/validation/select-validation">
<ion-label>
Select Validation Test
</ion-label>
</ion-item>
</ion-list>
<ion-list>

View File

@ -0,0 +1,63 @@
<ion-header>
<ion-toolbar>
<ion-title>Select - Validation Test</ion-title>
</ion-toolbar>
</ion-header>
<ion-content class="ion-padding">
<div class="validation-info">
<h2>Screen Reader Testing Instructions:</h2>
<ol>
<li>Enable your screen reader (VoiceOver, NVDA, JAWS, etc.)</li>
<li>Tab through the form fields</li>
<li>When you tab away from an empty required field, the error should be announced immediately</li>
<li>The error text should be announced BEFORE the next field is announced</li>
<li>Test in Chrome, Safari, and Firefox to verify consistent behavior</li>
</ol>
</div>
<form [formGroup]="form">
<div class="grid">
<div>
<h2>Required Field</h2>
<ion-select
#fruitsSelect
id="fruits-select"
[label]="fieldMetadata.fruits.label"
placeholder="Select one"
interface="alert"
[helperText]="fieldMetadata.fruits.helperText"
[errorText]="fieldMetadata.fruits.errorText"
formControlName="fruits"
required
>
<ion-select-option value="apples">Apples</ion-select-option>
<ion-select-option value="oranges">Oranges</ion-select-option>
<ion-select-option value="pears">Pears</ion-select-option>
</ion-select>
</div>
<div>
<h2>Optional Field (No Validation)</h2>
<ion-select
#optionalSelect
id="optional-select"
[label]="fieldMetadata.optional.label"
placeholder="Select one"
interface="alert"
[helperText]="fieldMetadata.optional.helperText"
formControlName="optional"
>
<ion-select-option value="red">Red</ion-select-option>
<ion-select-option value="blue">Blue</ion-select-option>
<ion-select-option value="green">Green</ion-select-option>
</ion-select>
</div>
</div>
</form>
<div class="ion-padding">
<ion-button id="submit-btn" expand="block" [disabled]="form.invalid" (click)="onSubmit()">Submit Form</ion-button>
<ion-button id="reset-btn" expand="block" fill="outline" (click)="form.reset()">Reset Form</ion-button>
</div>
</ion-content>

View File

@ -0,0 +1,36 @@
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
grid-row-gap: 20px;
grid-column-gap: 20px;
}
h2 {
font-size: 12px;
font-weight: normal;
color: var(--ion-color-step-600);
margin-top: 10px;
margin-bottom: 5px;
}
.validation-info {
margin: 20px;
padding: 10px;
background: var(--ion-color-light);
border-radius: 4px;
}
.validation-info h2 {
font-size: 14px;
font-weight: 600;
margin-bottom: 10px;
}
.validation-info ol {
margin: 0;
padding-left: 20px;
}
.validation-info li {
margin-bottom: 5px;
}

View File

@ -0,0 +1,63 @@
import { CommonModule } from '@angular/common';
import { Component } from '@angular/core';
import {
FormBuilder,
ReactiveFormsModule,
Validators
} from '@angular/forms';
import {
IonButton,
IonContent,
IonHeader,
IonSelect,
IonSelectOption,
IonTitle,
IonToolbar
} from '@ionic/angular/standalone';
@Component({
selector: 'app-select-validation',
templateUrl: './select-validation.component.html',
styleUrls: ['./select-validation.component.scss'],
standalone: true,
imports: [
CommonModule,
ReactiveFormsModule,
IonSelect,
IonSelectOption,
IonButton,
IonHeader,
IonToolbar,
IonTitle,
IonContent
]
})
export class SelectValidationComponent {
// Field metadata for labels and error messages
fieldMetadata = {
fruits: {
label: 'Fruits',
helperText: "Select an option",
errorText: 'This field is required'
},
optional: {
label: 'Colors',
helperText: 'You can skip this field',
errorText: ''
}
};
form = this.fb.group({
fruits: ['', Validators.required],
optional: ['']
});
constructor(private fb: FormBuilder) {}
// Submit form
onSubmit(): void {
if (this.form.valid) {
alert('Form submitted successfully!');
}
}
}