Skip to content

Commit

Permalink
Merge pull request #2039 from alphagov/remove-error-summary-hack
Browse files Browse the repository at this point in the history
Remove error summary override `init` function
  • Loading branch information
Vanita Barrett-Smith authored Feb 8, 2022
2 parents 21af9ad + ea7d0df commit c77662e
Show file tree
Hide file tree
Showing 12 changed files with 254 additions and 29 deletions.
20 changes: 20 additions & 0 deletions src/components/error-summary/default/code.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
title: Error summary
layout: layout-example.njk
---

{% from "govuk/components/error-summary/macro.njk" import govukErrorSummary %}

{{ govukErrorSummary({
titleText: "There is a problem",
errorList: [
{
text: "The date your passport was issued must be in the past",
href: "#passport-issued-day"
},
{
text: "Enter a postcode, like AA1 1AA",
href: "#postcode-input"
}
]
}) }}
1 change: 1 addition & 0 deletions src/components/error-summary/default/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ layout: layout-example.njk

{{ govukErrorSummary({
titleText: "There is a problem",
disableAutoFocus: true,
errorList: [
{
text: "The date your passport was issued must be in the past",
Expand Down
72 changes: 72 additions & 0 deletions src/components/error-summary/full-page-example/code.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
title: Full page example
layout: layout-example-full-page.njk
---

{% extends "example-wrappers/full-page.njk" %}

{% from "govuk/components/back-link/macro.njk" import govukBackLink %}
{% from "govuk/components/button/macro.njk" import govukButton %}
{% from "govuk/components/date-input/macro.njk" import govukDateInput %}
{% from "govuk/components/error-summary/macro.njk" import govukErrorSummary %}

{% block beforeContent %}
{{ govukBackLink({
text: "Back"
}) }}
{% endblock %}

{% block content %}
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<form action="/form-handler" method="post" novalidate>
{{ govukErrorSummary({
titleText: "There is a problem",
errorList: [
{
text: "Passport issue date must include a year",
href: "#passport-issued-year"
}
]
}) }}

{{ govukDateInput({
fieldset: {
legend: {
isPageHeading: true,
text: 'When was your passport issued?',
classes: 'govuk-fieldset__legend--l'
}
},
id: 'passport-issued',
namePrefix: 'passport-issued',
errorMessage: {
text: "Passport issue date must include a year"
},
items: [
{
name: "day",
classes: "govuk-input--width-2",
value: 5
},
{
name: "month",
classes: "govuk-input--width-2",
value: 12
},
{
name: "year",
classes: "govuk-input--width-4 govuk-input--error"
}
]
})
}}

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

</form>
</div>
</div>
{% endblock %}
9 changes: 5 additions & 4 deletions src/components/error-summary/full-page-example/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ layout: layout-example-full-page.njk
<div class="govuk-grid-column-two-thirds">
<form action="/form-handler" method="post" novalidate>
{{ govukErrorSummary({
"titleText": "There is a problem",
"errorList": [
titleText: "There is a problem",
disableAutoFocus: true,
errorList: [
{
"text": "Passport issue date must include a year",
"href": "#passport-issued-year"
text: "Passport issue date must include a year",
href: "#passport-issued-year"
}
]
}) }}
Expand Down
10 changes: 5 additions & 5 deletions src/components/error-summary/index.md.njk
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Use this component at the top of a page to summarise any errors a user has made.

When a user makes an error, you must show both an error summary and an [error message](/components/error-message/) next to each answer that contains an error.

{{ example({group: "components", item: "error-summary", example: "default", html: true, nunjucks: true, open: false, size: "s"}) }}
{{ example({group: "components", item: "error-summary", example: "default", customCode: true, html: true, nunjucks: true, open: false, size: "s"}) }}

## When to use this component

Expand Down Expand Up @@ -42,20 +42,20 @@ 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 file upload, select, textarea, text input or character count, link to the field.

{{ example({group: "components", item: "error-summary", example: "linking", html: true, nunjucks: true, open: false, size: "s"}) }}
{{ example({group: "components", item: "error-summary", example: "linking", customCode: true, html: true, nunjucks: true, open: false, size: "s"}) }}

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 first field that contains an error.

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

{{ example({group: "components", item: "error-summary", example: "linking-multiple-fields", html: true, nunjucks: true, open: false, size: "s"}) }}
{{ example({group: "components", item: "error-summary", example: "linking-multiple-fields", customCode: true, html: true, nunjucks: true, open: false, size: "s"}) }}

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

{{ example({group: "components", item: "error-summary", example: "linking-checkboxes-radios", html: true, nunjucks: true, open: false, size: "s"}) }}
{{ example({group: "components", item: "error-summary", example: "linking-checkboxes-radios", customCode: true, html: true, nunjucks: true, open: false, size: "s"}) }}

### 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>`.

{{ example({group: "components", item: "error-summary", example: "full-page-example", html: true, nunjucks: true, open: false, size: "s"}) }}
{{ example({group: "components", item: "error-summary", example: "full-page-example", customCode: true, html: true, nunjucks: true, open: false, size: "s"}) }}
53 changes: 53 additions & 0 deletions src/components/error-summary/linking-checkboxes-radios/code.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
title: Linking from the error summary to checkboxes
layout: layout-example.njk
---

{% from "govuk/components/error-summary/macro.njk" import govukErrorSummary %}
{% from "govuk/components/checkboxes/macro.njk" import govukCheckboxes %}

{{ govukErrorSummary({
titleText: "There is a problem",
errorList: [
{
text: "Select if you are British, Irish or a citizen of a different country",
href: "#nationality"
}
]
}) }}

{{ govukCheckboxes({
idPrefix: "nationality",
name: "nationality",
fieldset: {
legend: {
text: "What is your nationality?",
isPageHeading: true,
classes: "govuk-fieldset__legend--l"
}
},
hint: {
text: "If you have dual nationality, select all options that are relevant to you."
},
errorMessage: {
text: "Select if you are British, Irish or a citizen of a different country"
},
items: [
{
value: "british",
text: "British",
hint: {
text: "including English, Scottish, Welsh and Northern Irish"
}
},
{
value: "irish",
text: "Irish"
},
{
value: "other",
text: "Citizen of another country"
}
]
}) }}

Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ layout: layout-example.njk
{% from "govuk/components/checkboxes/macro.njk" import govukCheckboxes %}

{{ govukErrorSummary({
"titleText": "There is a problem",
"errorList": [
titleText: "There is a problem",
disableAutoFocus: true,
errorList: [
{
"text": "Select if you are British, Irish or a citizen of a different country",
"href": "#nationality"
text: "Select if you are British, Irish or a citizen of a different country",
href: "#nationality"
}
]
}) }}
Expand Down
49 changes: 49 additions & 0 deletions src/components/error-summary/linking-multiple-fields/code.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
title: Linking from the error summary to an answer that uses multiple fields
layout: layout-example.njk
---

{% from "govuk/components/date-input/macro.njk" import govukDateInput %}
{% from "govuk/components/error-summary/macro.njk" import govukErrorSummary %}

{{ govukErrorSummary({
titleText: "There is a problem",
errorList: [
{
text: "Passport issue date must include a year",
href: "#passport-issued-year"
}
]
}) }}

{{ govukDateInput({
fieldset: {
legend: {
isPageHeading: true,
text: 'When was your passport issued?',
classes: 'govuk-fieldset__legend--l'
}
},
id: 'passport-issued',
namePrefix: 'passport-issued',
errorMessage: {
text: "Passport issue date must include a year"
},
items: [
{
name: "day",
classes: "govuk-input--width-2",
value: 5
},
{
name: "month",
classes: "govuk-input--width-2",
value: 12
},
{
name: "year",
classes: "govuk-input--width-4 govuk-input--error"
}
]
})
}}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ layout: layout-example.njk
{% from "govuk/components/error-summary/macro.njk" import govukErrorSummary %}

{{ govukErrorSummary({
"titleText": "There is a problem",
"errorList": [
titleText: "There is a problem",
disableAutoFocus: true,
errorList: [
{
"text": "Passport issue date must include a year",
"href": "#passport-issued-year"
text: "Passport issue date must include a year",
href: "#passport-issued-year"
}
]
}) }}
Expand Down
32 changes: 32 additions & 0 deletions src/components/error-summary/linking/code.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
title: Linking from the error summary to an answer that uses a single field
layout: layout-example.njk
---

{% from "govuk/components/error-summary/macro.njk" import govukErrorSummary %}
{% from "govuk/components/input/macro.njk" import govukInput %}

{{ govukErrorSummary({
titleText: "There is a problem",
errorList: [
{
text: "Enter your full name",
href: "#full-name-input"
}
]
}) }}

<h1 class="govuk-heading-l">Your details</h1>

{{ govukInput({
label: {
text: 'Full name'
},
id: "full-name-input",
name: "name",
autocomplete: "name",
errorMessage: {
text: "Enter your full name"
}
}) }}

13 changes: 7 additions & 6 deletions src/components/error-summary/linking/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ layout: layout-example.njk
{% from "govuk/components/input/macro.njk" import govukInput %}

{{ govukErrorSummary({
"titleText": "There is a problem",
"errorList": [
titleText: "There is a problem",
disableAutoFocus: true,
errorList: [
{
"text": "Enter your full name",
"href": "#full-name-input"
text: "Enter your full name",
href: "#full-name-input"
}
]
}) }}
Expand All @@ -20,13 +21,13 @@ layout: layout-example.njk

{{ govukInput({
label: {
"text": 'Full name'
text: 'Full name'
},
id: "full-name-input",
name: "name",
autocomplete: "name",
errorMessage: {
"text": "Enter your full name"
text: "Enter your full name"
}
}) }}

6 changes: 0 additions & 6 deletions src/javascripts/govuk-frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ nodeListForEach($details, function ($detail) {
var $errorSummaries = document.querySelectorAll('[data-module="govuk-error-summary"]')
nodeListForEach($errorSummaries, function ($errorSummary) {
var errorSummary = new ErrorSummary($errorSummary)
// Override the `init` method since it automatically focuses the ErrorSummary.
// This is not ideal when showing examples for this component
// TODO: Allow option for ErrorSummary to avoid this hack
errorSummary.init = function () {
this.$module.addEventListener('click', ErrorSummary.prototype.handleClick.bind(this))
}
errorSummary.init()
})

Expand Down

0 comments on commit c77662e

Please sign in to comment.