Browse Source

Add out of range validation

pull/981/head
Kat 4 years ago
parent
commit
db13b70713
  1. 15
      app/controllers/locations_controller.rb
  2. 1
      config/locales/en.yml
  3. 9
      spec/requests/locations_controller_spec.rb

15
app/controllers/locations_controller.rb

@ -172,23 +172,28 @@ private
month = params[:location]["deactivation_date(2i)"] month = params[:location]["deactivation_date(2i)"]
year = params[:location]["deactivation_date(1i)"] 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) if !deactivation_date_valid?(day, month, year, collection_start_date)
set_deactivation_date_errors(day, month, year) set_deactivation_date_errors(day, month, year, collection_start_date)
else else
Date.new(year.to_i, month.to_i, day.to_i) Date.new(year.to_i, month.to_i, day.to_i)
end end
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?) if [day, month, year].any?(&:blank?)
{day:, month:, year:}.each do |period, value| {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? @location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.not_entered", period: period.to_s )) if value.blank?
end end
elsif !Date.valid_date?(year.to_i, month.to_i, day.to_i) 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")) @location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.invalid"))
elsif !year.to_i.between?(2000, 2200) 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.invalid")) @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 end
end end

1
config/locales/en.yml

@ -317,6 +317,7 @@ en:
not_selected: "Select one of the options" not_selected: "Select one of the options"
not_entered: "Enter a %{period}" not_entered: "Enter a %{period}"
invalid: "Enter a valid date" invalid: "Enter a valid date"
out_of_range: "The date must be on or after the %{date}"
soft_validations: soft_validations:
net_income: net_income:

9
spec/requests/locations_controller_spec.rb

@ -1296,6 +1296,15 @@ RSpec.describe LocationsController, type: :request do
end end
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 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"} }} let(:params) { { location: { "deactivation_date": "other", "deactivation_date(3i)": "", "deactivation_date(2i)": "2", "deactivation_date(1i)": "2022"} }}

Loading…
Cancel
Save