Forms
Error summary
A box at the top of the page that lists every problem after a form is submitted.
When to use
- After someone submits a form with problems.
When not to use
- Before someone has submitted the form.
- For messages that are not errors. Use a notice.
How to use
- Use the same words as the message next to each field.
- Link each item to its field.
- Start the page title with "Error: " while it shows.
- Add
data-ggds-module="error-summary": with the GGDS script, the page moves to the summary when it loads.
Accessibility
What GGDS does
- It has
role="alert", so screen readers announce it even without JavaScript. - With the GGDS script it takes focus when the page loads, and each link moves focus to its field with the question in view.
What you need to do
- List every error, in the same words as the message next to each field, and link each one to its field.
- Start the page
<title>with "Error: " while it shows. - Put it at the top of the main content, before the form.
Example: Form after a failed submit
<div class="ggds-error-summary" data-ggds-module="error-summary" role="alert" aria-labelledby="error-summary-title" tabindex="-1">
<h2 class="ggds-error-summary__title" id="error-summary-title">There is a problem</h2>
<ul class="ggds-error-summary__list">
<li><a href="#name">Enter your full name</a></li>
<li><a href="#reply-sms">Select how we should reply</a></li>
</ul>
</div>
<h1>Report a problem</h1>
<form action="./" method="post" novalidate>
<div class="ggds-field ggds-field--error">
<label class="ggds-label" for="name">Full name</label>
<p class="ggds-error-message" id="name-error"><span class="ggds-visually-hidden">Error:</span> Enter your full name</p>
<input class="ggds-input ggds-input--width-long" id="name" name="name" type="text" autocomplete="name" aria-invalid="true" aria-describedby="name-error">
</div>
<div class="ggds-field ggds-field--error">
<fieldset class="ggds-fieldset" aria-describedby="reply-error">
<legend class="ggds-fieldset__legend">How should we reply?</legend>
<p class="ggds-error-message" id="reply-error"><span class="ggds-visually-hidden">Error:</span> Select how we should reply</p>
<div class="ggds-choices">
<div class="ggds-choice">
<input class="ggds-choice__input" id="reply-sms" name="reply" type="radio" value="sms">
<label class="ggds-choice__label" for="reply-sms">SMS</label>
</div>
<div class="ggds-choice">
<input class="ggds-choice__input" id="reply-email" name="reply" type="radio" value="email">
<label class="ggds-choice__label" for="reply-email">Email</label>
</div>
</div>
</fieldset>
</div>
<button class="ggds-button" type="submit">Send report</button>
</form><>
<div className="ggds-error-summary" data-ggds-module="error-summary" role="alert" aria-labelledby="error-summary-title" tabIndex={-1}>
<h2 className="ggds-error-summary__title" id="error-summary-title">There is a problem</h2>
<ul className="ggds-error-summary__list">
<li><a href="#name">Enter your full name</a></li>
<li><a href="#reply-sms">Select how we should reply</a></li>
</ul>
</div>
<h1>Report a problem</h1>
<form action="./" method="post" noValidate>
<div className="ggds-field ggds-field--error">
<label className="ggds-label" htmlFor="name">Full name</label>
<p className="ggds-error-message" id="name-error"><span className="ggds-visually-hidden">Error:</span> Enter your full name</p>
<input className="ggds-input ggds-input--width-long" id="name" name="name" type="text" autoComplete="name" aria-invalid="true" aria-describedby="name-error" />
</div>
<div className="ggds-field ggds-field--error">
<fieldset className="ggds-fieldset" aria-describedby="reply-error">
<legend className="ggds-fieldset__legend">How should we reply?</legend>
<p className="ggds-error-message" id="reply-error"><span className="ggds-visually-hidden">Error:</span> Select how we should reply</p>
<div className="ggds-choices">
<div className="ggds-choice">
<input className="ggds-choice__input" id="reply-sms" name="reply" type="radio" value="sms" />
<label className="ggds-choice__label" htmlFor="reply-sms">SMS</label>
</div>
<div className="ggds-choice">
<input className="ggds-choice__input" id="reply-email" name="reply" type="radio" value="email" />
<label className="ggds-choice__label" htmlFor="reply-email">Email</label>
</div>
</div>
</fieldset>
</div>
<button className="ggds-button" type="submit">Send report</button>
</form>
</>Use it
- Stylesheet
ggds.cssincludes it. To load only the parts you use:core.css, thencomponents/error-summary.css(0.3 KB with gzip, minified).- JavaScript
- Optional. With data-ggds-module="error-summary", the GGDS script moves focus to the summary when the page loads, and each link moves focus to its field and scrolls its label or question into view.
HTML
Copy the HTML under each example above. It works in any website.
Astro
No Astro component: copy the HTML.
React and Next.js
With @ggds/react:
import { ErrorSummary } from '@ggds/react';
{/* First in <main>. It takes the focus when errors appear; nothing shows without errors. */}
<ErrorSummary errors={[{ href: '#phone', text: 'Enter your mobile phone number' }]} />Django
With ggds-django ({% load ggds %}):
{# Automatic with {{ form }} and GGDSFormRenderer; on its own: #}
{% ggds_error_summary form %}Classes and states
| Class | What it does |
|---|---|
.ggds-error-summary | The box, with a thick red border. Give it role="alert", aria-labelledby (its title) and tabindex="-1". |
.ggds-error-summary__title | "There is a problem". |
.ggds-error-summary__list | One link per problem, each to its field. |
| State | What it does |
|---|---|
:focus | Focused when the page loads (with the GGDS script). |
Accessibility tests
Automated, on every change
These run each time GGDS changes. A change that fails one is not accepted.
- WCAG 2.2 A and AA rules (axe), at desktop and phone widths, on every example
- No sideways scrolling on a 320px screen, also with a wide font, on every example
- Every approved colour pair meets its contrast minimum, for every institution theme
- No inline scripts or styles, so it runs under a strict Content Security Policy
- Behaviour test: “error summary takes focus on load”
- Behaviour test: “after a failed submit the error summary is read first, and its links reach each field”
By people, with assistive technology
Automated tests find only some of the barriers people meet. These checks are still to be done.
| Check | Result | Notes |
|---|---|---|
| Keyboard only | Not tested yet | Waiting for the external audit with disabled people (decision D-09). |
| NVDA screen reader with Firefox or Chrome (Windows) | Not tested yet | Waiting for the external audit with disabled people (decision D-09). |
| JAWS screen reader with Chrome (Windows) | Not tested yet | Waiting for the external audit with disabled people (decision D-09). |
| VoiceOver screen reader with Safari (iPhone) | Not tested yet | Waiting for the external audit with disabled people (decision D-09). |
| TalkBack screen reader with Chrome (Android) | Not tested yet | Waiting for the external audit with disabled people (decision D-09). |
| Zoom to 400%, and increased text spacing | Not tested yet | Waiting for the external audit with disabled people (decision D-09). |
| Windows contrast themes (forced colours) | Not tested yet | Waiting for the external audit with disabled people (decision D-09). |
| Voice control (Voice Access or Dragon) | Not tested yet | Waiting for the external audit with disabled people (decision D-09). |
Changes
| Version | Change |
|---|---|
| 0.4.0 | React and Astro: the banner's register link and the hero's search label now default to GGDS's own words, so you no longer have to pass them. React has errorTitle() for the page title while a form has errors. |
| 0.1.0 | The error summary no longer scrolls the page around it when shown inside a frame. |
| 0.1.0 | New: ggds-django, for Django and Django-based CMSs such as Wagtail: the page parts as template tags, a form renderer that turns any Django form into GGDS markup with an error summary and accessible errors, and the GGDS styles, themes and script as static files. |
| 0.1.0 | Added. |
Related components
Something wrong, unclear or missing? or suggest a change to it on GitHub. You need a GitHub account.