diff --git a/app/controllers/form_controller.rb b/app/controllers/form_controller.rb
index 95da341fc..f0c632c3f 100644
--- a/app/controllers/form_controller.rb
+++ b/app/controllers/form_controller.rb
@@ -15,8 +15,9 @@ class FormController < ApplicationController
redirect_to(send(redirect_path, @case_log))
end
else
- @subsection = @case_log.form.subsection_for_page(@page)
- render "form/page", status: :unprocessable_entity
+ redirect_path = "case_log_#{@page.id}_path"
+ session[:errors] = @case_log.errors.to_json
+ redirect_to(send(redirect_path, @case_log))
end
else
render_not_found
@@ -37,6 +38,11 @@ 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, message|
+ @case_log.errors.add field.to_sym, message.first
+ end
+ end
@subsection = @case_log.form.subsection_for_page(page)
@page = @case_log.form.get_page(page.id)
if @page.routed_to?(@case_log) && @page.subsection.enabled?(@case_log)
diff --git a/app/views/form/page.html.erb b/app/views/form/page.html.erb
index d946c3e2d..ab006ccdc 100644
--- a/app/views/form/page.html.erb
+++ b/app/views/form/page.html.erb
@@ -9,7 +9,7 @@
>
<% remove_other_page_errors(@case_log, @page) %>
diff --git a/spec/features/form/validations_spec.rb b/spec/features/form/validations_spec.rb
index 34f6fbfd5..fa74ac10c 100644
--- a/spec/features/form/validations_spec.rb
+++ b/spec/features/form/validations_spec.rb
@@ -60,51 +60,51 @@ RSpec.describe "validations" do
describe "date validation", js: true do
def fill_in_date(case_log_id, question, day, month, year, path)
visit("/logs/#{case_log_id}/#{path}")
- fill_in("#{question}_1i", with: year)
- fill_in("#{question}_2i", with: month)
- fill_in("#{question}_3i", with: day)
+ fill_in("case_log[#{question}(1i)]", with: year)
+ fill_in("case_log[#{question}(2i)]", with: month)
+ fill_in("case_log[#{question}(3i)]", with: day)
end
it "does not allow out of range dates to be submitted" do
- fill_in_date(id, "case_log_mrcdate", 3100, 12, 2000, "property-major-repairs")
+ fill_in_date(id, "mrcdate", 3100, 12, 2000, "property-major-repairs")
click_button("Save and continue")
expect(page).to have_current_path("/logs/#{id}/property-major-repairs")
- fill_in_date(id, "case_log_mrcdate", 12, 1, 20_000, "property-major-repairs")
+ fill_in_date(id, "mrcdate", 12, 1, 20_000, "property-major-repairs")
click_button("Save and continue")
expect(page).to have_current_path("/logs/#{id}/property-major-repairs")
- fill_in_date(id, "case_log_mrcdate", 13, 100, 2020, "property-major-repairs")
+ fill_in_date(id, "mrcdate", 13, 100, 2020, "property-major-repairs")
click_button("Save and continue")
expect(page).to have_current_path("/logs/#{id}/property-major-repairs")
- fill_in_date(id, "case_log_mrcdate", 21, 11, 2020, "property-major-repairs")
+ fill_in_date(id, "mrcdate", 21, 11, 2020, "property-major-repairs")
click_button("Save and continue")
expect(page).to have_current_path("/logs/#{id}/local-authority/check-answers")
end
it "does not allow non numeric inputs to be submitted" do
- fill_in_date(id, "case_log_mrcdate", "abc", "de", "ff", "property-major-repairs")
+ fill_in_date(id, "mrcdate", "abc", "de", "ff", "property-major-repairs")
click_button("Save and continue")
expect(page).to have_current_path("/logs/#{id}/property-major-repairs")
end
it "does not allow partial inputs to be submitted" do
- fill_in_date(id, "case_log_mrcdate", 21, 12, nil, "property-major-repairs")
+ fill_in_date(id, "mrcdate", 21, 12, nil, "property-major-repairs")
click_button("Save and continue")
expect(page).to have_current_path("/logs/#{id}/property-major-repairs")
- fill_in_date(id, "case_log_mrcdate", 12, nil, 2000, "property-major-repairs")
+ fill_in_date(id, "mrcdate", 12, nil, 2000, "property-major-repairs")
click_button("Save and continue")
expect(page).to have_current_path("/logs/#{id}/property-major-repairs")
- fill_in_date(id, "case_log_mrcdate", nil, 10, 2020, "property-major-repairs")
+ fill_in_date(id, "mrcdate", nil, 10, 2020, "property-major-repairs")
click_button("Save and continue")
expect(page).to have_current_path("/logs/#{id}/property-major-repairs")
end
it "allows valid inputs to be submitted" do
- fill_in_date(id, "case_log_mrcdate", 21, 11, 2020, "property-major-repairs")
+ fill_in_date(id, "mrcdate", 21, 11, 2020, "property-major-repairs")
click_button("Save and continue")
expect(page).to have_current_path("/logs/#{id}/local-authority/check-answers")
end
diff --git a/spec/requests/form_controller_spec.rb b/spec/requests/form_controller_spec.rb
index 1fc4ceb12..8c7ea8de8 100644
--- a/spec/requests/form_controller_spec.rb
+++ b/spec/requests/form_controller_spec.rb
@@ -129,7 +129,7 @@ RSpec.describe FormController, type: :request do
let(:answer) { 2000 }
it "re-renders the same page with errors if validation fails" do
- expect(response).to have_http_status(:unprocessable_entity)
+ expect(response).to redirect_to("/logs/#{case_log.id}/#{page_id.dasherize}")
end
end