-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #447 from x-govuk/add-title-with-error-prefix
Add helper: title_with_error_prefix
- Loading branch information
Showing
8 changed files
with
100 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module TitleWithErrorPrefixHelper | ||
def title_with_error_prefix(title, error:, error_prefix: Govuk::Components.config.default_error_prefix) | ||
"#{error_prefix if error}#{title}" | ||
end | ||
end | ||
|
||
ActiveSupport.on_load(:action_view) { include TitleWithErrorPrefixHelper } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--- | ||
title: Title with error prefix | ||
--- | ||
|
||
markdown: | ||
If you have a page containing a form, you should add the "Error: " prefix to the page title if there is a validation error on the page. | ||
|
||
This means that screen readers will read it out as soon as possible. | ||
|
||
To do this, you can use the `title_with_error_prefix` helper. | ||
|
||
If you are using a Rails form, you can set the error attribute by calling `errors.any?` on the form object: | ||
|
||
``` | ||
= title_with_error_prefix("Personal details", error: @personal_details_form.errors.any?) | ||
``` | ||
|
||
== render('/partials/example.*', | ||
caption: "Title with an error present", | ||
code: title_containing_error) | ||
|
||
== render('/partials/example.*', | ||
caption: "Title with an error present", | ||
code: title_not_containing_error) | ||
|
||
== render('/partials/example.*', | ||
caption: "Title with a custom error prefix", | ||
code: title_containing_error_and_custom_error_prefix) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require 'ostruct' | ||
|
||
module Examples | ||
module TitleWithErrorPrefixHelpers | ||
def title_containing_error | ||
<<~TITLE_WITH_ERROR_PREFIX | ||
= title_with_error_prefix("Personal details", error: true) | ||
TITLE_WITH_ERROR_PREFIX | ||
end | ||
|
||
def title_not_containing_error | ||
<<~TITLE_WITH_ERROR_PREFIX | ||
= title_with_error_prefix("Personal details", error: false) | ||
TITLE_WITH_ERROR_PREFIX | ||
end | ||
|
||
def title_containing_error_and_custom_error_prefix | ||
<<~TITLE_WITH_ERROR_PREFIX | ||
= title_with_error_prefix("Manylion personol", error: true, error_prefix: "Gwall: ") | ||
TITLE_WITH_ERROR_PREFIX | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe(TitleWithErrorPrefixHelper, type: 'helper') do | ||
describe '#title_with_error_prefix' do | ||
context 'when there is an error' do | ||
subject { title_with_error_prefix('What is their name?', error: true) } | ||
|
||
it 'should include the Error: prefix' do | ||
expect(subject).to eql('Error: What is their name?') | ||
end | ||
end | ||
|
||
context 'when there is not an error' do | ||
subject { title_with_error_prefix('What is their name?', error: false) } | ||
|
||
it 'should not include a prefix' do | ||
expect(subject).to eql('What is their name?') | ||
end | ||
end | ||
|
||
context 'when error is not specified' do | ||
subject { title_with_error_prefix('What is their name?') } | ||
|
||
it 'should raise an argument error' do | ||
expect { subject }.to raise_error(ArgumentError) | ||
end | ||
end | ||
|
||
context 'when an alternative error prefix is given' do | ||
subject { title_with_error_prefix('What is their name?', error: true, error_prefix: "Opps: ") } | ||
|
||
it 'should include the alternative error prefix' do | ||
expect(subject).to eql('Opps: What is their name?') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters