|
|
|
|
@ -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 |
|
|
|
|
|