diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index 06ed0a064..a6cfe475c 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -26,7 +26,7 @@ class LocationsController < ApplicationController elsif params[:location][:confirm].present? && params[:location][:deactivation_date].present? confirm_deactivation else - deactivation_date_errors + @location.deactivation_date_errors(params) if @location.errors.present? @location.deactivation_date_type = params[:location][:deactivation_date_type] render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity @@ -170,28 +170,6 @@ private redirect_to scheme_location_path(@scheme, @location) end - def deactivation_date_errors - if params[:location][:deactivation_date].blank? && params[:location][:deactivation_date_type].blank? - @location.errors.add(:deactivation_date_type, message: I18n.t("validations.location.deactivation_date.not_selected")) - end - - if params[:location][:deactivation_date_type] == "other" - day = params[:location]["deactivation_date(3i)"] - 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?) - @location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.not_entered")) - 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 !Date.new(year.to_i, month.to_i, day.to_i).between?(collection_start_date, Date.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 - def deactivation_date return if params[:location].blank? diff --git a/app/models/location.rb b/app/models/location.rb index a6b044cfa..51824c2a9 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -382,6 +382,28 @@ class Location < ApplicationRecord status == :active end + def deactivation_date_errors(params) + if params[:location][:deactivation_date].blank? && params[:location][:deactivation_date_type].blank? + @location.errors.add(:deactivation_date_type, message: I18n.t("validations.location.deactivation_date.not_selected")) + end + + if params[:location][:deactivation_date_type] == "other" + day = params[:location]["deactivation_date(3i)"] + 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?) + @location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.not_entered")) + 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 !Date.new(year.to_i, month.to_i, day.to_i).between?(collection_start_date, Date.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 + private PIO = PostcodeService.new