Browse Source

Check date input format

pull/2918/head
Kat 3 months ago
parent
commit
ad13e8730b
  1. 3
      app/controllers/form_controller.rb
  2. 2
      config/locales/en.yml
  3. 2
      config/locales/validations/shared.en.yml
  4. 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("/") day, month, year = params[@log.log_type][question.id].split("/")
next unless [day, month, year].any?(&:present?) 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) Date.new(year.to_i, month.to_i, day.to_i)
else else
Date.new(0, 1, 1) Date.new(0, 1, 1)

2
config/locales/en.yml

@ -124,7 +124,7 @@ en:
location: location:
attributes: attributes:
startdate: 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: units:
blank: "Enter the total number of units at this location." blank: "Enter the total number of units at this location."
type_of_unit: 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." postcode: "Enter a postcode in the correct format, for example AA1 1AA."
date: 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."

23
spec/requests/form_controller_spec.rb

@ -619,6 +619,25 @@ RSpec.describe FormController, type: :request do
end end
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 context "when allow_future_form_use? is enabled" do
before do before do
allow(FeatureToggle).to receive(:allow_future_form_use?).and_return(true) 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, id: lettings_log.id,
lettings_log: { lettings_log: {
page: page_id, page: page_id,
"startdate" => "1/1/1", "startdate" => "1/1/1000",
}, },
} }
end end
@ -652,7 +671,7 @@ RSpec.describe FormController, type: :request do
id: sales_log.id, id: sales_log.id,
sales_log: { sales_log: {
page: page_id, page: page_id,
"saledate" => "1/1/1", "saledate" => "1/1/1000",
}, },
} }
end end

Loading…
Cancel
Save