Browse Source

Fix checkbox validation (#823)

* Don't crash when invalid checkbox answers are selected

* Ensure all checkbox selections are preserved not just the first that doesn't match the all

* Rubocop
pull/824/head v0.2.5
baarkerlounger 2 years ago committed by GitHub
parent
commit
b0aa8611d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      app/controllers/form_controller.rb
  2. 23
      spec/features/form/checkboxes_spec.rb

5
app/controllers/form_controller.rb

@ -73,7 +73,7 @@ private
end
if session["fields"]
session["fields"].each do |field, value|
unless @case_log.form.get_question(field, @case_log).type == "date"
unless @case_log.form.get_question(field, @case_log)&.type == "date"
@case_log[field] = value
end
end
@ -161,10 +161,11 @@ private
def question_missing_response?(responses_for_page, question)
if %w[checkbox validation_override].include?(question.type)
question.answer_options.keys.reject { |x| x.match(/divider/) }.all? do |option|
answered = question.answer_options.keys.reject { |x| x.match(/divider/) }.map do |option|
session["fields"][option] = @case_log[option] = params["case_log"][question.id].include?(option) ? 1 : 0
params["case_log"][question.id].exclude?(option)
end
answered.all?
else
session["fields"][question.id] = @case_log[question.id] = responses_for_page[question.id]
responses_for_page[question.id].nil? || responses_for_page[question.id].blank?

23
spec/features/form/checkboxes_spec.rb

@ -38,4 +38,27 @@ RSpec.describe "Checkboxes" do
expect(case_log["housingneeds_h"]).to eq(1)
end
end
context "when a checkbox question is submitted with invalid answers" do
before do
allow(case_log).to receive(:update).and_return(false)
end
it "shows an error summary" do
visit("/logs/#{id}/accessibility-requirements")
page.check("case-log-accessibility-requirements-housingneeds-a-field")
page.check("case-log-accessibility-requirements-housingneeds-c-field")
click_button("Save and continue")
expect(page).to have_title("Error")
end
it "persists the original selections" do
visit("/logs/#{id}/accessibility-requirements")
page.check("case-log-accessibility-requirements-housingneeds-a-field")
page.check("case-log-accessibility-requirements-housingneeds-c-field")
click_button("Save and continue")
expect(page).to have_checked_field("case-log-accessibility-requirements-housingneeds-a-field")
expect(page).to have_checked_field("case-log-accessibility-requirements-housingneeds-c-field")
end
end
end

Loading…
Cancel
Save