x-govuk/govuk-form-builder

Highlighting single date fields which are in error

Opened this issue · 3 comments

The Design System's guidance for Date Inputs has a subsection about error messages. This contains the following recommendations (abridged slightly):

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

If nothing is entered: Highlight the date input as a whole.
If the date is incomplete: Highlight the day, month or year field where the information is missing or incomplete. If more than one field is missing information, highlight the fields the user needs to fill in.
If the date entered cannot be correct: For example, ‘13’ in the month field cannot be correct. Highlight the day, month or year field with the incorrect information. Or highlight the date as a whole if there’s incorrect information in more than one field, or it’s not clear which field is incorrect.
If the date is in the future when it needs to be in the past: Highlight the date input as a whole.
If the date is in the future when it needs to be today or in the past: Highlight the date input as a whole.
If the date is in the past when it needs to be in the future: Highlight the date input as a whole.
If the date is in the past when it needs to be today or in the future: Highlight the date input as a whole.
If the date must be the same as or after another date: Highlight the date input as a whole.
If the date must be after another date: Highlight the date input as a whole.
If the date must be the same as or before another date: Highlight the date input as a whole.
If the date must be before another date: Highlight the date input as a whole.
If the date must be between two dates: Highlight the date input as a whole.

Is there a known/recommended way to highlight a single date field that's in error? As far as I can tell there's no easy way of doing this with the gem's date input component.

There isn't because the field is bound to a date object which as far as Ruby is concerned is either valid or invalid.

I've been thinking about the best way of checking it, as have others, but taking on the role of actually validating the dates goes beyond the scope of this library.

It might be the case that the builder will display a certain format of errors but the building of them is the responsibility of the app - or via a separate gem.

I'm definitely open to collaborating on a nice, reusable solution for this. cc @felixclack

Began experimenting with ways to address this in #462.

Some work has been done on this front:

  • #489 - this issue pointed out that hardcoding the day/month/year segments as 3i, 2i and 1i respectively forced Rails to try and cast them to integer, removing the i makes it easier to take control of the parsing
  • #493 - add a config option to configure the date segments at the app level
  • #493 - allow date segments to be overridden when calling govuk_date_field

Now it's possible to fully customise the errors. Here's LAA-submit-crime-forms: