Skip to content

Commit

Permalink
Add hidden "Error:" prefix to error messages
Browse files Browse the repository at this point in the history
We've standardised on using a simpler, more 'instructional' language for our messages, but this means that most error messages are less explicit about the fact that the field is in an error state.

I think we are therefore relying slightly more on the visual presentation of the error message to help the user understand that they have 'done something wrong'.

Thinking specifically about the way that a screen-reader user would encounter an error message:

> National Insurance number, text field. It’s on your National Insurance card, benefit letter, payslip or P60. For example, ‘QQ 12 34 56 C’. Enter a National Insurance number in the right format. You are currently inside a text field. To enter text in this field, type.

I don't think it's entirely clear that "Enter a National Insurance number in the right format" is an error.

This attempts to counter that by adding a visually hidden "Error:" prefix to the error message:

> National Insurance number, text field. It’s on your National Insurance card, benefit letter, payslip or P60. For example, ‘QQ 12 34 56 C’. Error: Enter a National Insurance number in the right format. You are currently inside a text field. To enter text in this field, type.
  • Loading branch information
36degrees committed Mar 1, 2019
1 parent 4446431 commit 85a5d3f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/components/error-message/error-message.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ params:
type: object
required: false
description: HTML attributes (for example data attributes) to add to the error message span tag
- name: visuallyHiddenText
type: string
description: A visually hidden prefix used before the error message. Defaults to "Error".

accessibilityCriteria: |
When used with a single input, the error message MUST:
Expand Down
3 changes: 3 additions & 0 deletions src/components/error-message/template.njk
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{% set visuallyHiddenText = params.visuallyHiddenText | default("Error") -%}

<span {%- if params.id %} id="{{ params.id }}"{% endif %} class="govuk-error-message{%- if params.classes %} {{ params.classes }}{% endif %}" {%- for attribute, value in params.attributes %} {{ attribute }}="{{ value }}"{% endfor -%}>
{% if visuallyHiddenText %}<span class="govuk-visually-hidden">{{ visuallyHiddenText }}:</span> {% endif -%}
{{ params.html | safe if params.html else params.text }}
</span>
33 changes: 31 additions & 2 deletions src/components/error-message/template.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('Error message', () => {
})

const content = $('.govuk-error-message').html().trim()
expect(content).toEqual('Unexpected &gt; in body')
expect(content).toContain('Unexpected &gt; in body')
})

it('allows summary HTML to be passed un-escaped', () => {
Expand All @@ -38,7 +38,7 @@ describe('Error message', () => {
})

const content = $('.govuk-error-message').html().trim()
expect(content).toEqual('Unexpected <b>bold text</b> in body copy')
expect(content).toContain('Unexpected <b>bold text</b> in body copy')
})

it('allows additional attributes to be specified', () => {
Expand All @@ -53,4 +53,33 @@ describe('Error message', () => {
expect($component.attr('data-test')).toEqual('attribute')
expect($component.attr('id')).toEqual('my-error-message')
})

it('includes a visually hidden "Error" prefix by default', () => {
const $ = render('error-message', {
text: 'Enter your full name'
})

const $component = $('.govuk-error-message')
expect($component.text().trim()).toEqual('Error: Enter your full name')
})

it('allows the visually hidden prefix to be customised', () => {
const $ = render('error-message', {
text: 'Rhowch eich enw llawn',
visuallyHiddenText: 'Gwall'
})

const $component = $('.govuk-error-message')
expect($component.text().trim()).toEqual('Gwall: Rhowch eich enw llawn')
})

it('allows the visually hidden prefix to be removed', () => {
const $ = render('error-message', {
text: 'There is an error on line 42',
visuallyHiddenText: false
})

const $component = $('.govuk-error-message')
expect($component.text().trim()).toEqual('There is an error on line 42')
})
})

0 comments on commit 85a5d3f

Please sign in to comment.