diff --git a/app/controllers/case_logs_controller.rb b/app/controllers/case_logs_controller.rb index de7501375..edd0cc4be 100644 --- a/app/controllers/case_logs_controller.rb +++ b/app/controllers/case_logs_controller.rb @@ -24,21 +24,27 @@ class CaseLogsController < ApplicationController def next_page form = Form.new(2021, 2022) @case_log = CaseLog.find(params[:case_log_id]) + previous_page = params[:previous_page] questions_for_page = form.questions_for_page(previous_page).keys answers_for_page = page_params(questions_for_page).select { |k, _v| questions_for_page.include?(k) } - if @case_log.valid? + + @case_log_temp = CaseLog.new(answers_for_page) + if @case_log_temp.valid? @case_log.update!(answers_for_page) next_page = form.next_page(previous_page) redirect_to(send("case_log_#{next_page}_path", @case_log)) + else + @errors = @case_log_temp.errors.full_messages + redirect_to(send("case_log_#{previous_page}_path", @case_log, errors: @errors)) end end form = Form.new(2021, 2022) form.all_pages.map do |page_key, page_info| - define_method(page_key) do + define_method(page_key) do |errors = {}| @case_log = CaseLog.find(params[:case_log_id]) - render "form/page", locals: { case_log_id: @case_log.id, form: form, page_key: page_key, page_info: page_info } + render "form/page", locals: { case_log_id: @case_log.id, form: form, page_key: page_key, page_info: page_info, errors: errors } end end diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 01092a240..d4321572c 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -5,11 +5,12 @@ class CaseLogValidator < ActiveModel::Validator elsif record.tenant_age > 120 record.errors.add :base, "Age needs to be below 120" end - binding.pry end def validate(record) - validate_tenant_age(record) + if record.tenant_age? + validate_tenant_age(record) + end end end diff --git a/app/views/form/page.html.erb b/app/views/form/page.html.erb index 997587d37..d6343d2cf 100644 --- a/app/views/form/page.html.erb +++ b/app/views/form/page.html.erb @@ -6,12 +6,17 @@ <% end %> <%= turbo_frame_tag "case_log_form", target: "_top" do %> + <% if page_info["header"].present? %>

<%= page_info["header"] %>

<% end %> <%= form_with action: '/case_logs', method: "next_page", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %> + <% if params[:errors].present? %> + <%= params[:errors] %> + <%= f.govuk_error_summary 'Uh-oh, spaghettios' %> + <% end %> <% page_info["questions"].map do |question_key, question| %> <%= render partial: "form/#{question["type"]}_question", locals: { question_key: question_key, question: question, f: f } %>