Aggregating Form Errors
Collect per-field and form-level errors into one structure, order them by severity, and render an accessible error summary that links to each field.
On this page
Introduction
This guide covers aggregating form errors into one accessible structure, a recurring concern in the form-validation scenario. A form with several invalid fields needs to tell the user what is wrong in one place, not as a scatter of red borders the user has to hunt. An error summary that lists each problem and links to the field is both more accessible and faster to fix, and it falls out of a single error structure you already build during validation.
Collect every issue
Run the validator once and gather all issues with their field path and message, so the summary reflects the full state rather than the first error.
The trade-off worth naming is that run the validator once and gather all issues with their field path and message, so the summary reflects the full state rather than the first error adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
function collect(result): { path: string; msg: string; level: 'error'|'warn' }[] {
if (result.success) return [];
return result.error.issues.map(i => ({ path: i.path.join('.'), msg: i.message, level: 'error' }));
}
Sort by severity
Order blocking errors before warnings so a screen reader user hears what stops submit first, which is the order they need to fix things.
The trade-off worth naming is that order blocking errors before warnings so a screen reader user hears what stops submit first, which is the order they need to fix things adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Render the summary
Put the summary at the top of the form with a heading and a list, and give it aria-live so a revalidation announces changes without moving focus unexpectedly.
The trade-off worth naming is that put the summary at the top of the form with a heading and a list, and give it aria-live so a revalidation announces changes without moving focus unexpectedly adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
<section aria-labelledby="errh">
<h2 id="errh">Please fix the following</h2>
<ul>
<li><a href="#email">Email is invalid</a></li>
<li><a href="#age">Age must be positive</a></li>
</ul>
</section>
Link to the field
Give each input an id and each summary item an href to it, so clicking an error moves focus to the field, which is faster than scrolling and hunting.
The trade-off worth naming is that give each input an id and each summary item an href to it, so clicking an error moves focus to the field, which is faster than scrolling and hunting adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Announce revalidation
Use aria-live polite on the summary so a revalidation that clears errors announces the change, but never assertive, which would interrupt a typing user.
The trade-off worth naming is that use aria-live polite on the summary so a revalidation that clears errors announces the change, but never assertive, which would interrupt a typing user adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Test the summary
Assert the summary lists every invalid field, the first item links to the first field, and a cleared field removes its item, so the summary stays honest.
The trade-off worth naming is that assert the summary lists every invalid field, the first item links to the first field, and a cleared field removes its item, so the summary stays honest adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Sources
Sources: WAI-ARIA error summary, Zod error formatting.
FAQ
- Should the error summary list warnings too?
- Yes, but after blocking errors and with a distinct label, so a screen reader user hears what stops submit first and what is optional second.
- How do I link a summary item to its field?
- Give the input an id and the summary item an href to that id, so clicking moves focus and assistive tech announces the target field.
Report a problem with this articleOpens a pre-filled issue on GitHub.