Browse Source

don't display invalid date inputs when getting a validation error (#807)

pull/808/head
kosiakkatrina 2 years ago committed by GitHub
parent
commit
a380b1b320
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 22
      app/controllers/form_controller.rb
  2. 39
      spec/features/form/page_routing_spec.rb

22
app/controllers/form_controller.rb

@ -48,12 +48,7 @@ class FormController < ApplicationController
form.pages.map do |page|
define_method(page.id) do |_errors = {}|
if @case_log
if session["errors"]
JSON(session["errors"]).each do |field, messages|
messages.each { |message| @case_log.errors.add field.to_sym, message }
end
end
session["fields"].each { |field, value| @case_log[field] = value } if session["fields"]
restore_error_field_values
@subsection = @case_log.form.subsection_for_page(page)
@page = @case_log.form.get_page(page.id)
if @page.routed_to?(@case_log, current_user)
@ -70,6 +65,21 @@ class FormController < ApplicationController
private
def restore_error_field_values
if session["errors"]
JSON(session["errors"]).each do |field, messages|
messages.each { |message| @case_log.errors.add field.to_sym, message }
end
end
if session["fields"]
session["fields"].each do |field, value|
unless @case_log.form.get_question(field, @case_log).type == "date"
@case_log[field] = value
end
end
end
end
def responses_for_page(page)
page.questions.each_with_object({}) do |question, result|
question_params = params["case_log"][question.id]

39
spec/features/form/page_routing_spec.rb

@ -72,4 +72,43 @@ RSpec.describe "Form Page Routing" do
expect(page).to have_current_path("/logs/#{id}/property-wheelchair-accessible")
end
end
context "when answer is invalid" do
it "shows error with invalid value in the field" do
visit("/logs/#{id}/property-postcode")
fill_in("case-log-postcode-full-field", with: "fake_postcode")
click_button("Save and continue")
expect(page).to have_current_path("/logs/#{id}/property-postcode")
expect(find("#case-log-postcode-full-field-error").value).to eq("fake_postcode")
end
it "does not reset the displayed date" do
case_log.update!(startdate: "2021/10/13")
visit("/logs/#{id}/tenancy-start-date")
fill_in("case_log[startdate(1i)]", with: "202")
fill_in("case_log[startdate(2i)]", with: "32")
fill_in("case_log[startdate(3i)]", with: "0")
click_button("Save and continue")
expect(page).to have_current_path("/logs/#{id}/tenancy-start-date")
expect(find_field("case_log[startdate(3i)]").value).to eq("13")
expect(find_field("case_log[startdate(2i)]").value).to eq("10")
expect(find_field("case_log[startdate(1i)]").value).to eq("2021")
end
it "does not reset the displayed date if it's empty" do
case_log.update!(startdate: nil)
visit("/logs/#{id}/tenancy-start-date")
fill_in("case_log[startdate(1i)]", with: "202")
fill_in("case_log[startdate(2i)]", with: "32")
fill_in("case_log[startdate(3i)]", with: "0")
click_button("Save and continue")
expect(page).to have_current_path("/logs/#{id}/tenancy-start-date")
expect(find_field("case_log[startdate(3i)]").value).to eq(nil)
expect(find_field("case_log[startdate(2i)]").value).to eq(nil)
expect(find_field("case_log[startdate(1i)]").value).to eq(nil)
end
end
end

Loading…
Cancel
Save