Skip to content

Commit

Permalink
Support error_items for specific input within date-input
Browse files Browse the repository at this point in the history
As suggested in #3510.

This changes the date-input component so it supports errors on
individual input elements, e.g. just the year input element. This usage
is described in the docs for the date-input component [1]:

> If you’re highlighting just one field - either the day, month or year
> - only style the field that has an error. The error message must say
> which field has an error, like this:

However, highlighting just one field like this is not actually currently
supported by the date-input component!

The new behaviour (where individual fields are highlighted as having an
error) is specified by adding values for a `name` key to the
`error_items` with the values matching the values of the `name` key in
the `items`. If none of the `error_items` have a value for the `name`
key, then we fallback to the original behaviour, i.e. all fields are
highlighted as having an error. This should hopefully make this a fairly
backwardly-compatible change.

[1]: https://design-system.service.gov.uk/components/date-input/#error-messages
  • Loading branch information
floehopper committed Sep 25, 2023
1 parent e0dcdec commit aa4439e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
error_items ||= []
describedby ||= nil
has_error ||= error_message || error_items.any?
error_item_names = error_items.group_by { |ei| ei[:name] }.keys.compact_blank
has_no_named_errors = error_item_names.none?

css_classes = %w(gem-c-date-input govuk-date-input)
form_group_css_classes = %w(govuk-form-group)
Expand Down Expand Up @@ -59,7 +61,7 @@
text: item[:label] || item[:name].capitalize
},
grouped: true,
has_error: has_error,
has_error: error_item_names.include?(item[:name]) || (has_error && has_no_named_errors),
name: name ? (name + "[" + item[:name] + "]") : item[:name],
value: item[:value],
width: item[:width],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,10 @@ examples:
name: dob-blwyddyn
width: 4
value: 1980
with_named_error_items:
data:
legend_text: "When was your passport issued?"
hint: "For example, 27 3 2007"
error_items:
- name: "year"
text: "The date your passport was issued must include a year"
16 changes: 16 additions & 0 deletions spec/components/date_input_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ def component_name
assert_select ".govuk-form-group--error .govuk-fieldset[role=group] .govuk-date-input__item .govuk-input--error", 3
end

it "renders with named error items" do
render_component(
legend_text: "What is your date of birth?",
error_items: [
{ name: "day", text: "day-error" },
{ name: "year", text: "year-error" },
],
)

assert_select(
".govuk-form-group--error .govuk-fieldset[role=group] .govuk-error-message",
html: "<span class=\"govuk-visually-hidden\">Error:</span> day-error<br>year-error",
)
assert_select ".govuk-form-group--error .govuk-fieldset[role=group] .govuk-date-input__item .govuk-input--error", 2
end

it "renders with custom items" do
render_component(
legend_text: "What is your date of birth?",
Expand Down

0 comments on commit aa4439e

Please sign in to comment.