From a380b1b320f4db70aa7dd2b7f87405f3b7ad1000 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Wed, 3 Aug 2022 14:05:19 +0100 Subject: [PATCH] don't display invalid date inputs when getting a validation error (#807) --- app/controllers/form_controller.rb | 22 ++++++++++---- spec/features/form/page_routing_spec.rb | 39 +++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/app/controllers/form_controller.rb b/app/controllers/form_controller.rb index 936551749..5a8d6375d 100644 --- a/app/controllers/form_controller.rb +++ b/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] diff --git a/spec/features/form/page_routing_spec.rb b/spec/features/form/page_routing_spec.rb index 644bbb608..abf65dad3 100644 --- a/spec/features/form/page_routing_spec.rb +++ b/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