Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date part specific errors #462

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Conversation

peteryates
Copy link
Member

@peteryates peteryates commented Jan 14, 2024

The GOV.UK Design System now recommends that when part of a date is incorrect it should be individually highlighted.

This is problematic because:

  • A date is a single thing, it is either valid or invalid
  • When multiple things could be wrong which do we highlight? (currently it will just show the first one in date/month/year order)
  • Ruby's Date class, which is used by Rails, doesn't have any concept of somewhat-valid dates

The problems above mean we need to treat dates as a special case and developers will need to intercept Rails' validation and do their own.

This change adds support for showing custom error messages and highlighting the appropriate fields in the form builder. It does it by checking date fields for extra errors on the field that end with _day, _month or _year (i.e. retirement_date_year). If they are present:

  • the date field's form group will be marked as containing errors
  • the error message will be shown
  • the input at fault will get the govuk-input-error class and a red
  • the error summary will link to the input with the error

Screenshot from 2024-01-14 15-53-48

Problems

  • adding magic attributes that we check for error messages isn't very robust, if a developer adds both payment_date and payment_date_year to their object and exposes both in the form the error summary will likely link to both
  • there's a lot of work and additional testing required by teams to implement this custom logic and special validation
  • the validation needs to be applied in a particular way. The example in the Design System documentation shows a date without a year. Rails will try to parse the input into a Date very early on - before the model gets hold of it to apply validation. Handling this safely and cleanly is non-trivial and beyond the scope of this library

Changes

Copy link

netlify bot commented Jan 14, 2024

Deploy Preview for govuk-form-builder ready!

Name Link
🔨 Latest commit 6d68e87
🔍 Latest deploy log https://app.netlify.com/sites/govuk-form-builder/deploys/65a408be077ecd00081a79c6
😎 Deploy Preview https://deploy-preview-462--govuk-form-builder.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

None yet

1 participant