Browse Source

CLDC-3855 Check date input format (#2918)

* Check date input format

* Update feature test

* Check if the date is valid before running location validations
pull/2932/head
kosiakkatrina 1 month ago committed by GitHub
parent
commit
5ae962696f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 3
      app/controllers/form_controller.rb
  2. 5
      app/models/validations/setup_validations.rb
  3. 2
      config/locales/en.yml
  4. 2
      config/locales/validations/shared.en.yml
  5. 2
      spec/features/form/page_routing_spec.rb
  6. 23
      spec/requests/form_controller_spec.rb

3
app/controllers/form_controller.rb

@ -141,7 +141,8 @@ private
day, month, year = params[@log.log_type][question.id].split("/")
next unless [day, month, year].any?(&:present?)
result[question.id] = if Date.valid_date?(year.to_i, month.to_i, day.to_i) && year.to_i.positive?
date_matches_format = params[@log.log_type][question.id].match?(/\A\d{1,2}\/\d{1,2}\/\d{4}\z/)
result[question.id] = if date_matches_format && Date.valid_date?(year.to_i, month.to_i, day.to_i) && year.to_i.positive?
Date.new(year.to_i, month.to_i, day.to_i)
else
Date.new(0, 1, 1)

5
app/models/validations/setup_validations.rb

@ -97,6 +97,8 @@ module Validations::SetupValidations
end
def location_during_startdate_validation(record)
return unless date_valid?("startdate", record)
location_inactive_status = inactive_status(record.startdate, record.location)
if location_inactive_status.present?
@ -108,6 +110,8 @@ module Validations::SetupValidations
end
def scheme_during_startdate_validation(record)
return unless date_valid?("startdate", record)
scheme_inactive_status = inactive_status(record.startdate, record.scheme)
if scheme_inactive_status.present?
@ -120,6 +124,7 @@ module Validations::SetupValidations
def tenancy_startdate_with_scheme_locations(record)
return if record.scheme.blank? || record.startdate.blank?
return if record.scheme.has_active_locations_on_date?(record.startdate)
return unless date_valid?("startdate", record)
record.errors.add :startdate, I18n.t("validations.lettings.setup.startdate.scheme.locations_inactive.startdate", name: record.scheme.service_name)
record.errors.add :scheme_id, I18n.t("validations.lettings.setup.startdate.scheme.locations_inactive.scheme_id", name: record.scheme.service_name)

2
config/locales/en.yml

@ -124,7 +124,7 @@ en:
location:
attributes:
startdate:
invalid: "Enter a date in the correct format, for example 31 1 2022."
invalid: "Enter a date in the correct format, for example 31/1/2022."
units:
blank: "Enter the total number of units at this location."
type_of_unit:

2
config/locales/validations/shared.en.yml

@ -18,4 +18,4 @@ en:
postcode: "Enter a postcode in the correct format, for example AA1 1AA."
date:
invalid_date: "Enter a date in the correct format, for example 31 1 2024."
invalid_date: "Enter a date in the correct format, for example 31/1/2024."

2
spec/features/form/page_routing_spec.rb

@ -105,7 +105,7 @@ RSpec.describe "Form Page Routing" do
it "displays the entered date if it's in a valid format" do
lettings_log.update!(startdate: "2021/10/13")
visit("/lettings-logs/#{id}/tenancy-start-date")
fill_in("lettings_log[startdate]", with: "1/12/202")
fill_in("lettings_log[startdate]", with: "1/12/0202")
click_button("Save and continue")
expect(page).to have_current_path("/lettings-logs/#{id}/tenancy-start-date")

23
spec/requests/form_controller_spec.rb

@ -619,6 +619,25 @@ RSpec.describe FormController, type: :request do
end
end
context "when the date input doesn't match the required format" do
let(:page_id) { "tenancy_start_date" }
let(:params) do
{
id: lettings_log.id,
lettings_log: {
page: page_id,
"startdate" => "31620224352342",
},
}
end
it "validates the date correctly" do
post "/lettings-logs/#{lettings_log.id}/#{page_id.dasherize}", params: params
follow_redirect!
expect(page).to have_content("There is a problem")
end
end
context "when allow_future_form_use? is enabled" do
before do
allow(FeatureToggle).to receive(:allow_future_form_use?).and_return(true)
@ -631,7 +650,7 @@ RSpec.describe FormController, type: :request do
id: lettings_log.id,
lettings_log: {
page: page_id,
"startdate" => "1/1/1",
"startdate" => "1/1/1000",
},
}
end
@ -652,7 +671,7 @@ RSpec.describe FormController, type: :request do
id: sales_log.id,
sales_log: {
page: page_id,
"saledate" => "1/1/1",
"saledate" => "1/1/1000",
},
}
end

Loading…
Cancel
Save