Sub topic 15
Making errors accessible
This chapter assumes you can build a form with useForm, register, and zodResolver, and that you read formState.errors to show messages. Here you will make those messages usable with a screen reader and a keyboard.
Why red text is not enough
A sighted user sees a red message under a field. A screen reader user hears nothing unless you tell the browser three things:
- Which field is invalid.
- Which message describes it.
- That a new message has appeared.
Connect the message to the field
Three attributes do most of the work:
| Attribute | Purpose |
|---|---|
| `aria-invalid` | Marks the input as failing validation. |
| `aria-describedby` | Points to the `id` of the error message, so the screen reader reads it with the field. |
| `role="alert"` | Makes the message announce itself when it appears. |
Every input also needs a real <label htmlFor> that matches its id. A placeholder is not a label.
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { z } from "zod";
const schema = z.object({
email: z.string().min(1, "Email is required").email("Enter a valid email"),
});
type Values = z.infer<typeof schema>;
export function EmailForm() {
const {
register,
handleSubmit,
formState: { errors },
} = useForm<Values>({
resolver: zodResolver(schema),
defaultValues: { email: "" },
});
return (
<form onSubmit={handleSubmit((values) => console.log(values))} noValidate>
<label htmlFor="email">Email</label>
<input
id="email"
type="email"
aria-invalid={errors.email ? "true" : "false"}
aria-describedby={errors.email ? "email-error" : undefined}
{...register("email")}
/>
{errors.email && (
<p id="email-error" role="alert">
{errors.email.message}
</p>
)}
<button type="submit">Create account</button>
</form>
);
}The describedby value is set only while an error exists. This avoids pointing at an element that is not in the page.
What happens on a failed submit
React Hook Form's shouldFocusError option defaults to true. After a failed submit it focuses the first invalid field that registered a ref. That field's aria-describedby message is then read to the user. Keep the default unless you handle focus yourself.
Common mistakes
- Using
noValidateand then not showing any messages. The browser's built-in bubbles are gone, so your messages are the only feedback. - Showing an error through color alone. Include the text.
- Leaving
aria-describedbypointing at anidthat does not exist. - Rendering the error container only after the error appears while also relying on
aria-live. Live regions are more reliable when the container already exists.role="alert"on a newly added element works in most screen readers, so choose one approach and test it.
Exercise
Exercise
A password input shows its error message in a <p> below it, but a screen reader user hears only "Password, edit text" when focus lands there. Which two attributes would you add, and where?
Show answer
Add id="password-error" to the error <p>, and aria-describedby="password-error" to the input, only while the error exists. Also add aria-invalid="true" to the input so it is announced as invalid. Adding role="alert" to the <p> makes the message announce when it first appears.