Skip to content

Instantly share code, notes, and snippets.

@goodviber
Created November 12, 2021 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goodviber/06dd4fa6e4b7f2aa141eefc3aa142e75 to your computer and use it in GitHub Desktop.
Save goodviber/06dd4fa6e4b7f2aa141eefc3aa142e75 to your computer and use it in GitHub Desktop.
Gov uk form builder date error
<%= form.govuk_date_field :some_date,
legend: { text: t(".some_date", count: ordinal_number(@dose)), size: 's' },
hint_text: t('hint_texts.date', date_example: 1.week.ago.strftime('%d %m %Y')),
pre_fill_day: params.dig(:form_model, 'some_date(3i)'),
pre_fill_month: params.dig(:form_model, 'some_date(2i)'),
pre_fill_year: params.dig(:form_model, 'some_date(1i)')
%>
# /lib/extensions/gov_uk_design_system_form_builder
module GOVUKDesignSystemFormBuilder
module Builder
# Overriding govuk_date_field with parameters to pass through values to pre-fill date field
# For scenario when a user enters an invalid date, e.g. 31/2/2021, the page is re-rendered
# with an error message, if handled, but the date field would be populated with 3/3/2021.
# This allows us to pass through date actually entered from params and display it correctly.
def govuk_date_field(
attribute_name,
hint_text: nil,
legend: {},
pre_fill_day: nil,
pre_fill_month: nil,
pre_fill_year: nil,
date_of_birth: false,
omit_day: false,
&block
)
Elements::Date.new(
self,
object_name,
attribute_name,
hint_text: hint_text,
legend: legend,
pre_fill_day: pre_fill_day,
pre_fill_month: pre_fill_month,
pre_fill_year: pre_fill_year,
date_of_birth: date_of_birth,
omit_day: omit_day,
&block
).html
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment