Browse Source

No turbo so redirect form response

pull/406/head
baarkerlounger 3 years ago
parent
commit
0f75f15af0
  1. 10
      app/controllers/form_controller.rb
  2. 2
      app/views/form/page.html.erb
  3. 24
      spec/features/form/validations_spec.rb
  4. 2
      spec/requests/form_controller_spec.rb

10
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)

2
app/views/form/page.html.erb

@ -9,7 +9,7 @@
<div data-controller="govukfrontend"></div>
<%= form_with model: @case_log, url: form_case_log_path(@case_log), method: "post" do |f| %>
<%= form_with model: @case_log, url: form_case_log_path(@case_log), method: "post", local: true do |f| %>
<div class="govuk-grid-row">
<div class=<%= @page.questions[0].type == "interruption_screen" ? "govuk-grid-column-full-from-desktop" : "govuk-grid-column-two-thirds-from-desktop"%>>
<% remove_other_page_errors(@case_log, @page) %>

24
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

2
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

Loading…
Cancel
Save