From ad13e8730bb87aa3bd88376022e33736c7566804 Mon Sep 17 00:00:00 2001 From: Kat <54268893+kosiakkatrina@users.noreply.github.com> Date: Wed, 29 Jan 2025 09:36:48 +0000 Subject: [PATCH] Check date input format --- app/controllers/form_controller.rb | 3 ++- config/locales/en.yml | 2 +- config/locales/validations/shared.en.yml | 2 +- spec/requests/form_controller_spec.rb | 23 +++++++++++++++++++++-- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/app/controllers/form_controller.rb b/app/controllers/form_controller.rb index 63c37918d..e2b7f59ac 100644 --- a/app/controllers/form_controller.rb +++ b/app/controllers/form_controller.rb @@ -141,7 +141,8 @@ private day, month, year = params[@log.log_type][question.id].split("/") next unless [day, month, year].any?(&:present?) - result[question.id] = if Date.valid_date?(year.to_i, month.to_i, day.to_i) && year.to_i.positive? + date_matches_format = params[@log.log_type][question.id].match?(/\A\d{1,2}\/\d{1,2}\/\d{4}\z/) + result[question.id] = if date_matches_format && Date.valid_date?(year.to_i, month.to_i, day.to_i) && year.to_i.positive? Date.new(year.to_i, month.to_i, day.to_i) else Date.new(0, 1, 1) diff --git a/config/locales/en.yml b/config/locales/en.yml index 155814574..6ca3ea322 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -124,7 +124,7 @@ en: location: attributes: startdate: - invalid: "Enter a date in the correct format, for example 31 1 2022." + invalid: "Enter a date in the correct format, for example 31/1/2022." units: blank: "Enter the total number of units at this location." type_of_unit: diff --git a/config/locales/validations/shared.en.yml b/config/locales/validations/shared.en.yml index ea06636dc..ab0a532d4 100644 --- a/config/locales/validations/shared.en.yml +++ b/config/locales/validations/shared.en.yml @@ -18,4 +18,4 @@ en: postcode: "Enter a postcode in the correct format, for example AA1 1AA." date: - invalid_date: "Enter a date in the correct format, for example 31 1 2024." + invalid_date: "Enter a date in the correct format, for example 31/1/2024." diff --git a/spec/requests/form_controller_spec.rb b/spec/requests/form_controller_spec.rb index 497f65ca0..f5c4e6b19 100644 --- a/spec/requests/form_controller_spec.rb +++ b/spec/requests/form_controller_spec.rb @@ -619,6 +619,25 @@ RSpec.describe FormController, type: :request do end end + context "when the date input doesn't match the required format" do + let(:page_id) { "tenancy_start_date" } + let(:params) do + { + id: lettings_log.id, + lettings_log: { + page: page_id, + "startdate" => "31620224352342", + }, + } + end + + it "validates the date correctly" do + post "/lettings-logs/#{lettings_log.id}/#{page_id.dasherize}", params: params + follow_redirect! + expect(page).to have_content("There is a problem") + end + end + context "when allow_future_form_use? is enabled" do before do allow(FeatureToggle).to receive(:allow_future_form_use?).and_return(true) @@ -631,7 +650,7 @@ RSpec.describe FormController, type: :request do id: lettings_log.id, lettings_log: { page: page_id, - "startdate" => "1/1/1", + "startdate" => "1/1/1000", }, } end @@ -652,7 +671,7 @@ RSpec.describe FormController, type: :request do id: sales_log.id, sales_log: { page: page_id, - "saledate" => "1/1/1", + "saledate" => "1/1/1000", }, } end