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/app/models/validations/setup_validations.rb b/app/models/validations/setup_validations.rb index 8d1884fc2..6fd2c1a95 100644 --- a/app/models/validations/setup_validations.rb +++ b/app/models/validations/setup_validations.rb @@ -97,6 +97,8 @@ module Validations::SetupValidations end def location_during_startdate_validation(record) + return unless date_valid?("startdate", record) + location_inactive_status = inactive_status(record.startdate, record.location) if location_inactive_status.present? @@ -108,6 +110,8 @@ module Validations::SetupValidations end def scheme_during_startdate_validation(record) + return unless date_valid?("startdate", record) + scheme_inactive_status = inactive_status(record.startdate, record.scheme) if scheme_inactive_status.present? @@ -120,6 +124,7 @@ module Validations::SetupValidations def tenancy_startdate_with_scheme_locations(record) return if record.scheme.blank? || record.startdate.blank? return if record.scheme.has_active_locations_on_date?(record.startdate) + return unless date_valid?("startdate", record) record.errors.add :startdate, I18n.t("validations.lettings.setup.startdate.scheme.locations_inactive.startdate", name: record.scheme.service_name) record.errors.add :scheme_id, I18n.t("validations.lettings.setup.startdate.scheme.locations_inactive.scheme_id", name: record.scheme.service_name) 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/features/form/page_routing_spec.rb b/spec/features/form/page_routing_spec.rb index 72505d688..470a2f8ed 100644 --- a/spec/features/form/page_routing_spec.rb +++ b/spec/features/form/page_routing_spec.rb @@ -105,7 +105,7 @@ RSpec.describe "Form Page Routing" do it "displays the entered date if it's in a valid format" do lettings_log.update!(startdate: "2021/10/13") visit("/lettings-logs/#{id}/tenancy-start-date") - fill_in("lettings_log[startdate]", with: "1/12/202") + fill_in("lettings_log[startdate]", with: "1/12/0202") click_button("Save and continue") expect(page).to have_current_path("/lettings-logs/#{id}/tenancy-start-date") 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