From db13b707130ae47bf118247eb23f872ee5465468 Mon Sep 17 00:00:00 2001 From: Kat Date: Thu, 10 Nov 2022 12:40:22 +0000 Subject: [PATCH] Add out of range validation --- app/controllers/locations_controller.rb | 15 ++++++++++----- config/locales/en.yml | 1 + spec/requests/locations_controller_spec.rb | 9 +++++++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index 7592a5ac2..c11236026 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -172,23 +172,28 @@ private month = params[:location]["deactivation_date(2i)"] year = params[:location]["deactivation_date(1i)"] + collection_start_date = FormHandler.instance.current_collection_start_date - if [day, month, year].any?(&:blank?) || !Date.valid_date?(year.to_i, month.to_i, day.to_i) || !year.to_i.between?(2000, 2200) - set_deactivation_date_errors(day, month, year) + if !deactivation_date_valid?(day, month, year, collection_start_date) + set_deactivation_date_errors(day, month, year, collection_start_date) else Date.new(year.to_i, month.to_i, day.to_i) end end - def set_deactivation_date_errors(day, month, year) + def deactivation_date_valid?(day, month, year, collection_start_date) + [day, month, year].all?(&:present?) && Date.valid_date?(year.to_i, month.to_i, day.to_i) && Date.new(year.to_i, month.to_i, day.to_i).between?(collection_start_date, Time.new(2200,1,1)) + end + + def set_deactivation_date_errors(day, month, year, collection_start_date) if [day, month, year].any?(&:blank?) {day:, month:, year:}.each do |period, value| @location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.not_entered", period: period.to_s )) if value.blank? end elsif !Date.valid_date?(year.to_i, month.to_i, day.to_i) @location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.invalid")) - elsif !year.to_i.between?(2000, 2200) - @location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.invalid")) + elsif !Date.new(year.to_i, month.to_i, day.to_i).between?(collection_start_date, Time.new(2200,1,1)) + @location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.out_of_range", date: collection_start_date.to_formatted_s(:govuk_date))) end end end diff --git a/config/locales/en.yml b/config/locales/en.yml index d0aa9371a..bc0b0121b 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -317,6 +317,7 @@ en: not_selected: "Select one of the options" not_entered: "Enter a %{period}" invalid: "Enter a valid date" + out_of_range: "The date must be on or after the %{date}" soft_validations: net_income: diff --git a/spec/requests/locations_controller_spec.rb b/spec/requests/locations_controller_spec.rb index 5cc06f7be..e74ffb691 100644 --- a/spec/requests/locations_controller_spec.rb +++ b/spec/requests/locations_controller_spec.rb @@ -1296,6 +1296,15 @@ RSpec.describe LocationsController, type: :request do end end + context "when the date is entered is before the beginning of current collection window" do + let(:params) { { location: { "deactivation_date": "other", "deactivation_date(3i)": "10", "deactivation_date(2i)": "4", "deactivation_date(1i)": "2020"} }} + + it "displays the new page with an error message" do + expect(response).to have_http_status(:unprocessable_entity) + expect(page).to have_content(I18n.t("validations.location.deactivation_date.out_of_range", date: "5 April 2022")) + end + end + context "when the day is not entered" do let(:params) { { location: { "deactivation_date": "other", "deactivation_date(3i)": "", "deactivation_date(2i)": "2", "deactivation_date(1i)": "2022"} }}