From 7cc21aef4c18fd6494d3d98af84fc4744db4a2e4 Mon Sep 17 00:00:00 2001 From: Kat Date: Thu, 10 Nov 2022 11:21:09 +0000 Subject: [PATCH] Update errors --- app/controllers/locations_controller.rb | 19 +++++++++-- config/locales/en.yml | 3 +- spec/requests/locations_controller_spec.rb | 39 ++++++++++++++++++---- 3 files changed, 51 insertions(+), 10 deletions(-) diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index 62668a05e..7592a5ac2 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -172,10 +172,23 @@ private month = params[:location]["deactivation_date(2i)"] year = params[:location]["deactivation_date(1i)"] - if [day, month, year].all?(&:present?) && Date.valid_date?(year.to_i, month.to_i, day.to_i) && year.to_i.between?(2000, 2200) - Date.new(year.to_i, month.to_i, day.to_i) + + 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) else - @location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.not_entered")) + Date.new(year.to_i, month.to_i, day.to_i) + end + end + + def set_deactivation_date_errors(day, month, year) + 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")) end end end diff --git a/config/locales/en.yml b/config/locales/en.yml index a7843e4c5..e8bf5eca4 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -315,7 +315,8 @@ en: location: deactivation_date: not_selected: "Select one of the options" - not_entered: "Enter a date" + not_entered: "Enter a %{period}" + invalid: "Enter a valid date" soft_validations: net_income: diff --git a/spec/requests/locations_controller_spec.rb b/spec/requests/locations_controller_spec.rb index d771b9da4..5cc06f7be 100644 --- a/spec/requests/locations_controller_spec.rb +++ b/spec/requests/locations_controller_spec.rb @@ -1278,21 +1278,48 @@ RSpec.describe LocationsController, type: :request do end end - context "when the date is not entered" do - let(:params) { { location: { "deactivation_date": "other", "deactivation_date(3i)": "", "deactivation_date(2i)": "", "deactivation_date(1i)": ""} }} + context "when the date is not selected" do + let(:params) { { location: { "deactivation_date": "" } }} 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.not_entered")) + expect(page).to have_content(I18n.t("validations.location.deactivation_date.not_selected")) end end - context "when the date is not selected" do - let(:params) { { location: { "deactivation_date": "" } }} + context "when invalid date is entered" do + let(:params) { { location: { "deactivation_date": "other", "deactivation_date(3i)": "10", "deactivation_date(2i)": "44", "deactivation_date(1i)": "2022"} }} 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.not_selected")) + expect(page).to have_content(I18n.t("validations.location.deactivation_date.invalid")) + 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"} }} + + it "displays 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.not_entered", period: "day")) + end + end + + context "when the month is not entered" do + let(:params) { { location: { "deactivation_date": "other", "deactivation_date(3i)": "2", "deactivation_date(2i)": "", "deactivation_date(1i)": "2022"} }} + + it "displays 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.not_entered", period: "month")) + end + end + + context "when the year is not entered" do + let(:params) { { location: { "deactivation_date": "other", "deactivation_date(3i)": "2", "deactivation_date(2i)": "2", "deactivation_date(1i)": ""} }} + + it "displays 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.not_entered", period: "year")) end end end