Form elements - Error summary

Include an error summary at the top of a page to summarise any mistakes a user has made.

Open this default error summary example in a new tab
Copy default error summary code
<div class="nhsuk-error-summary" aria-labelledby="error-summary-title" role="alert" tabindex="-1">
  <h2 class="nhsuk-error-summary__title" id="error-summary-title">
    There is a problem
  </h2>
  <div class="nhsuk-error-summary__body">
    <p>
      Describe the errors and how to correct them
    </p>
    <ul class="nhsuk-list nhsuk-error-summary__list" role="list">
      <li>
        <a href="#example-error-1">Date of birth must be in the past</a>
      </li>
    </ul>
  </div>
</div>
Close default error summary code
Nunjucks macro options

Use options to customise the appearance, content and behaviour of a component when using a macro, for example, changing the text.

Some options are required for the macro to work; these are marked as "Required" in the option description.

If you're using Nunjucks macros in production with "html" options, or ones ending with "html", you must sanitise the HTML to protect against cross-site scripting exploits.

Nunjucks arguments for default error summary
Name Type Required Description
titleText string true If `titleHtml` is set, this is not required. Text to use for the heading of the error summary block. If `titleHtml` is provided, `titleText` will be ignored.
titleHtml string true If `titleText` is set, this is not required. HTML to use for the heading of the error summary block. If `titleHtml` is provided, `titleText` will be ignored.
descriptionText string false Text to use for the description of the errors. If you set `descriptionHtml`, the component will ignore `descriptionText`.
descriptionHtml string false HTML to use for the description of the errors. If you set this option, the component will ignore `descriptionText`.
errorList array true Contains an array of error link items and all their available arguments.
errorList[].href string false Href attribute for the error link item. If provided item will be an anchor.
errorList[].text string true If `html` is set, this is not required. Text for the error link item. If `html` is provided, the `text` argument will be ignored.
errorList[].html string true If `text` is set, this is not required. HTML for the error link item. If `html` is provided, the `text` argument will be ignored.
errorList[].attributes object false HTML attributes (for example data attributes) to add to the error link anchor.
classes string false Classes to add to the error-summary container.
attributes object false HTML attributes (for example data attributes) to add to the error-summary container.
Copy default error summary code
{% from 'error-summary/macro.njk' import errorSummary %}

{{ errorSummary({
  titleText: "There is a problem",
  descriptionText: "Describe the errors and how to correct them",
  errorList: [
    {
      text: "Date of birth must be in the past",
      href: "#example-error-1"
    }
  ]
}) }}
Close default error summary code

When to use an error summary

When a user makes a mistake, you must show an error summary at the top of the page as well as an error message next to each answer that contains an error.

Always show an error summary when there is a validation error, even if there’s only 1 mistake.

How to use an error summary

You must:

  • add "Error: " to the beginning of the <title> so screen readers read it out as soon as possible
  • show an error summary at the top of a page
  • move the keyboard focus to the error summary (the NHS.UK frontend JavaScript will do this for you)
  • include the heading "There is a problem"
  • link to each of the answers that have validation errors
  • show the same error messages next to the inputs with errors

Follow GOV.UK Design System guidance on writing good error messages.

Linking from the error summary to each answer

You must link the errors in the error summary to the answer they relate to.

For questions that require a user to answer using a single field, like a select, textarea or text input, link to the field.

Open this link input error summary example in a new tab

When a user has to enter their answer into multiple fields, such as the day, month and year fields in the date input component, link to the 1st field that contains an error.

If you do not know which field contains an error, link to the 1st field.

Open this link multiple input error summary example in a new tab

For questions that require a user to select one or more options from a list using radios or checkboxes, link to the 1st radio or checkbox.

Open this link options error summary example in a new tab

Where to put the error summary

Put the error summary at the top of the main container. If your page includes breadcrumbs or a back link, place it below these, but above the <h1>.

Open this placement error summary example in a new tab
Copy placement error summary code
<div class="nhsuk-width-container">

  <div class="nhsuk-back-link">

    <a class="nhsuk-back-link__link" href="#">
      <svg class="nhsuk-icon nhsuk-icon__chevron-left" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" aria-hidden="true" height="24" width="24">
        <path d="M8.5 12c0-.3.1-.5.3-.7l5-5c.4-.4 1-.4 1.4 0s.4 1 0 1.4L10.9 12l4.3 4.3c.4.4.4 1 0 1.4s-1 .4-1.4 0l-5-5c-.2-.2-.3-.4-.3-.7z"></path>
      </svg>
      Go back</a>
  </div>

  <main class="nhsuk-main-wrapper" id="maincontent" role="main">

    <div class="nhsuk-grid-row">
      <div class="nhsuk-grid-column-two-thirds">
        <form>

          <div class="nhsuk-error-summary" aria-labelledby="error-summary-title" role="alert" tabindex="-1">
            <h2 class="nhsuk-error-summary__title" id="error-summary-title">
              There is a problem
            </h2>
            <div class="nhsuk-error-summary__body">
              <ul class="nhsuk-list nhsuk-error-summary__list" role="list">
                <li>
                  <a href="#dob-errors-year">Date of birth must include a year</a>
                </li>
              </ul>
            </div>
          </div>

          <div class="nhsuk-form-group nhsuk-form-group--error">

            <fieldset class="nhsuk-fieldset" aria-describedby="dob-errors-hint dob-errors-error" role="group">
              <legend class="nhsuk-fieldset__legend nhsuk-fieldset__legend--l">
                <h1 class="nhsuk-fieldset__heading">
                  What is your date of birth?
                </h1>
              </legend>
              <div class="nhsuk-hint" id="dob-errors-hint">
                For example, 15 3 1984
              </div>

              <span class="nhsuk-error-message" id="dob-errors-error">
                <span class="nhsuk-u-visually-hidden">Error:</span> Date of birth must include a year
              </span>

              <div class="nhsuk-date-input" id="dob-errors">
                <div class="nhsuk-date-input__item">
                  <div class="nhsuk-form-group">
                    <label class="nhsuk-label nhsuk-date-input__label" for="dob-errors-day">
                      Day
                    </label>
                    <input class="nhsuk-input nhsuk-date-input__input nhsuk-input--width-2" id="dob-errors-day" name="day" type="text" value="15" inputmode="numeric">
                  </div>
                </div>
                <div class="nhsuk-date-input__item">
                  <div class="nhsuk-form-group">
                    <label class="nhsuk-label nhsuk-date-input__label" for="dob-errors-month">
                      Month
                    </label>
                    <input class="nhsuk-input nhsuk-date-input__input nhsuk-input--width-2" id="dob-errors-month" name="month" type="text" value="3" inputmode="numeric">
                  </div>
                </div>
                <div class="nhsuk-date-input__item">
                  <div class="nhsuk-form-group">
                    <label class="nhsuk-label nhsuk-date-input__label" for="dob-errors-year">
                      Year
                    </label>
                    <input class="nhsuk-input nhsuk-date-input__input nhsuk-input--width-4 nhsuk-input--error" id="dob-errors-year" name="year" type="text" inputmode="numeric">
                  </div>
                </div>
              </div>
            </fieldset>

          </div>

          <button class="nhsuk-button" data-module="nhsuk-button" type="submit">
            Continue
          </button>

        </form>
      </div>
    </div>

  </main>
</div>
Close placement error summary code
Nunjucks macro options

Use options to customise the appearance, content and behaviour of a component when using a macro, for example, changing the text.

Some options are required for the macro to work; these are marked as "Required" in the option description.

If you're using Nunjucks macros in production with "html" options, or ones ending with "html", you must sanitise the HTML to protect against cross-site scripting exploits.

Nunjucks arguments for placement error summary
Name Type Required Description
titleText string true If `titleHtml` is set, this is not required. Text to use for the heading of the error summary block. If `titleHtml` is provided, `titleText` will be ignored.
titleHtml string true If `titleText` is set, this is not required. HTML to use for the heading of the error summary block. If `titleHtml` is provided, `titleText` will be ignored.
descriptionText string false Text to use for the description of the errors. If you set `descriptionHtml`, the component will ignore `descriptionText`.
descriptionHtml string false HTML to use for the description of the errors. If you set this option, the component will ignore `descriptionText`.
errorList array true Contains an array of error link items and all their available arguments.
errorList[].href string false Href attribute for the error link item. If provided item will be an anchor.
errorList[].text string true If `html` is set, this is not required. Text for the error link item. If `html` is provided, the `text` argument will be ignored.
errorList[].html string true If `text` is set, this is not required. HTML for the error link item. If `html` is provided, the `text` argument will be ignored.
errorList[].attributes object false HTML attributes (for example data attributes) to add to the error link anchor.
classes string false Classes to add to the error-summary container.
attributes object false HTML attributes (for example data attributes) to add to the error-summary container.
Copy placement error summary code
{% from 'back-link/macro.njk' import backLink %}
{% from 'button/macro.njk' import button %}
{% from 'date-input/macro.njk' import dateInput %}
{% from 'error-summary/macro.njk' import errorSummary %}

{% block beforeContent %}
  {{ backLink({
    href: "#",
    text: "Go back"
  }) }}
{% endblock %}

{% block content %}
  <div class="nhsuk-grid-row">
    <div class="nhsuk-grid-column-two-thirds">
      <form>

        {{ errorSummary({
          titleText: "There is a problem",
          errorList: [
            {
              text: "Date of birth must include a year",
              href: "#dob-errors-year"
            }
          ]
        }) }}

        {{ dateInput({
          id: "dob-errors",
          fieldset: {
            legend: {
              text: "What is your date of birth?",
              classes: "nhsuk-fieldset__legend--l",
              isPageHeading: true
            }
          },
          hint: {
            text: "For example, 15 3 1984"
          },
          errorMessage: {
            text: "Date of birth must include a year"
          },
          items: [
            {
              name: "day",
              classes: "nhsuk-input--width-2",
              value: 15
            },
            {
              name: "month",
              classes: "nhsuk-input--width-2",
              value: 3
            },
            {
              name: "year",
              classes: "nhsuk-input--width-4 nhsuk-input--error"
            }
          ]
        }) }}

        {{ button({
          text: "Continue"
        }) }}

      </form>
    </div>
  </div>
{% endblock %}
Close placement error summary code

Research

GOV.UK research on error summaries showed users:

  • understood what went wrong
  • knew how to fix the problem
  • were able to recover from the error

Help us improve this guidance

Share insights or feedback and take part in the discussion. We use GitHub as a collaboration space. All the information on it is open to the public.

Read more about how to feedback or share insights.

If you have any questions, get in touch with the service manual team.

Updated: June 2024