x-govuk/govuk-form-builder

Date Handling Errors

Closed this issue · 6 comments

We're seeing the error invalid Date-like object: must be a Date, Time, DateTime or Hash in MULTIPARAMETER_KEY format
when inputting invalid dates to our form - both in browser and in tests.

To recreate this - have a 3 field date input and insert incorrect values for example Day: 1 - Month: 1 - Year: 20201

This worked previously in 2.4.0, is this expected in 2.5.0, or are we expected to change our error handling?

Yeah - I believe this is due to you using the GovUkDateFields gem, which looks like it's using this FormDate class to represent dates. It's not responding to #day, #month or #year (instead #dd, #mm, #yyyy) so it's not treated like a Ruby Date object.

The form builder sticks very closely to Rails' defaults, the 1i/2i/3i style params work with the inbuilt multi-parameter attributes to build a Date.

The options to resolve this are:

  • add a config option to the form builder that also checks the attribute for other accessors (here), so it might be something like config.custom_date_accessors = %i(dd mm yyyy)
  • patch govuk_uk_date_fields and add some alias_method to point #day at #dd etc
  • rework your date fields so they're more in-line with Rails - totally appreciate this might be a massive task so I'm more than happy to help with either of the two above if poss 😅

Also, cc @zheileman as he's familiar with both projects 🧞‍♂️

I had a chat with @zheileman and he was surprised people were still using the GovUkDateFields library, especially in conjunction with the form builder. We are of the opinion that moving to the form builder's (i.e. Rails') approach for handling dates and date params will be easiest in the long run.

As I said above - if it's a big task I'm willing to add an extra clause to the if statement linked above that'll allow you to continue using the latest version of the gem while you update your validations; I'll await a response before taking the next step.

Hi Peter - strange as we definitely don't use the GovUkDateFields library - it's definitely not in our yarn packs or gemfile. The issues must be arriving elsewhere?

Ah, sorry about that.

I think @RoseSAK and I made a bit of progress on this, looks like the issue is around the raising of a YearError here.

If you clone the form builder gem and add the path: to your Gemfile, you should be able to trace it through - when you add a big year like 20100, the attribute in the form builder is a block of HTML containing error details so the if statement that checks its format doesn't match anything and it'll fail with the 'invalid Date-like object' error.

v2.5.1b1 contains a new config option that will warn instead of fail when it encounters a date it doesn't like.

If the logs get a bit noisy you can shush them by toggling enable_logger.

I'll leave this option in place for the time being, but it'll remain undocumented.

This appears to have been resolved. The above PR/beta release wasn't necessary in the end. Closing for now.