diff --git a/app/controllers/form_controller.rb b/app/controllers/form_controller.rb index df924665a..656e0ab6e 100644 --- a/app/controllers/form_controller.rb +++ b/app/controllers/form_controller.rb @@ -7,11 +7,16 @@ class FormController < ApplicationController if @case_log @page = @case_log.form.get_page(params[:case_log][:page]) responses_for_page = responses_for_page(@page) - if @case_log.update(responses_for_page) + mandatory_questions_with_no_response = mandatory_questions_with_no_response(responses_for_page) + + if mandatory_questions_with_no_response.empty? && @case_log.update(responses_for_page) session[:errors] = nil redirect_to(successful_redirect_path) else redirect_path = "case_log_#{@page.id}_path" + mandatory_questions_with_no_response.map do |question| + @case_log.errors.add question.id.to_sym, I18n.t("validations.not_answered", question: question.display_label.downcase) + end session[:errors] = @case_log.errors.to_json Rails.logger.info "User triggered validation(s) on: #{@case_log.errors.map(&:attribute).join(', ')}" redirect_to(send(redirect_path, @case_log)) @@ -120,4 +125,13 @@ private redirect_path = @case_log.form.next_page_redirect_path(@page, @case_log, current_user) send(redirect_path, @case_log) end + + def mandatory_questions_with_no_response(responses_for_page) + @page.questions.select do |question| + (responses_for_page[question.id].nil? || responses_for_page[question.id].empty?) && + CaseLog::OPTIONAL_FIELDS.exclude?(question.id) && + @page.subsection.applicable_questions(@case_log).map(&:id).include?(question.id) + end + end end + diff --git a/app/models/form/question.rb b/app/models/form/question.rb index 35d776e6c..3d2f7a8a3 100644 --- a/app/models/form/question.rb +++ b/app/models/form/question.rb @@ -173,6 +173,10 @@ class Form::Question type == "radio" && RADIO_REFUSED_VALUE[id.to_sym]&.include?(value) end + def display_label + check_answer_label || header || id.humanize + end + private def selected_answer_option_is_derived?(case_log) diff --git a/config/locales/en.yml b/config/locales/en.yml index 0b76d9abf..37d04a013 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -42,6 +42,7 @@ en: organisation: name_missing: "Enter the organisation’s name" provider_type_missing: "Select the organisation’s type" + not_answered: "You must answer %{question}" other_field_missing: "If %{main_field_label} is other then %{other_field_label} must be provided" other_field_not_required: "%{other_field_label} must not be provided if %{main_field_label} was not other" diff --git a/spec/features/form/form_navigation_spec.rb b/spec/features/form/form_navigation_spec.rb index b2af0d190..b572e8783 100644 --- a/spec/features/form/form_navigation_spec.rb +++ b/spec/features/form/form_navigation_spec.rb @@ -56,17 +56,11 @@ RSpec.describe "Form Navigation" do pages = question_answers.map { |_key, val| val[:path] } pages[0..-2].each_with_index do |val, index| visit("/logs/#{id}/#{val}") - click_button("Save and continue") + click_link("Skip for now") expect(page).to have_current_path("/logs/#{id}/#{pages[index + 1]}") end end - it "a question page has a link allowing you to cancel your input and return to the check answers page" do - visit("logs/#{id}/tenant-code-test?referrer=check_answers") - click_link(text: "Cancel") - expect(page).to have_current_path("/logs/#{id}/setup/check-answers") - end - it "a question page has a Skip for now link that lets you move on to the next question without inputting anything" do visit("logs/#{empty_case_log.id}/tenant-code-test") click_link(text: "Skip for now") @@ -116,4 +110,57 @@ RSpec.describe "Form Navigation" do end end end + + describe "Editing a log" do + it "a question page has a link allowing you to cancel your input and return to the check answers page" do + visit("logs/#{id}/tenant-code-test?referrer=check_answers") + click_link(text: "Cancel") + expect(page).to have_current_path("/logs/#{id}/setup/check-answers") + end + + context "when clicking save and continue on a mandatory question with no input" do + let(:id) { empty_case_log.id } + + it "shows a validation error on radio questions" do + visit("/logs/#{id}/renewal") + click_button("Save and continue") + expect(page).to have_selector("#error-summary-title") + expect(page).to have_selector("#case-log-renewal-error") + expect(page).to have_title("Error") + end + + it "shows a validation error on date questions" do + visit("/logs/#{id}/tenancy-start-date") + click_button("Save and continue") + expect(page).to have_selector("#error-summary-title") + expect(page).to have_selector("#case-log-startdate-error") + expect(page).to have_title("Error") + end + + context "when the page has a main and conditional question" do + context "when the conditional question is required but not answered" do + xit "shows a validation error for the conditional question", js: true do + visit("/logs/#{id}/armed-forces") + choose("case-log-armedforces-1-field", allow_label_click: true) + click_button("Save and continue") + expect(page).to have_selector("#error-summary-title") + expect(page).to have_selector("#case-log-leftreg-error") + expect(page).to have_title("Error") + end + end + end + end + + context "when clicking save and continue on an optional question with no input" do + let(:id) { empty_case_log.id } + + it "does not show a validation error" do + visit("/logs/#{id}/tenant-code") + click_button("Save and continue") + expect(page).not_to have_selector("#error-summary-title") + expect(page).not_to have_title("Error") + expect(page).to have_current_path("/logs/#{id}/property-reference") + end + end + end end