Browse Source

Implement exclusive checkboxes (#281)

* Implement exclusive checkboxes

* Fix validation
pull/283/head
kosiakkatrina 3 years ago committed by GitHub
parent
commit
7f4312860c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      app/models/validations/household_validations.rb
  2. 3
      app/views/form/_checkbox_question.html.erb
  3. 41
      spec/features/form/checkboxes_spec.rb

2
app/models/validations/household_validations.rb

@ -65,7 +65,7 @@ module Validations::HouseholdValidations
if all_options.count("Yes") > 1 if all_options.count("Yes") > 1
mobility_accessibility_options = [record.housingneeds_a, record.housingneeds_b, record.housingneeds_c] mobility_accessibility_options = [record.housingneeds_a, record.housingneeds_b, record.housingneeds_c]
unless all_options.count("Yes") == 2 && record.housingneeds_f == "Yes" && mobility_accessibility_options.any? { |x| x == "Yes" } unless all_options.count("Yes") == 2 && record.housingneeds_f == "Yes" && mobility_accessibility_options.any? { |x| x == "Yes" }
record.errors.add :housingneeds_a, I18n.t("validations.household.housingneeds_a.one_or_two_choices") record.errors.add :accessibility_requirements, I18n.t("validations.household.housingneeds_a.one_or_two_choices")
end end
end end
end end

3
app/views/form/_checkbox_question.html.erb

@ -4,14 +4,17 @@
caption: caption(caption_text, page_header, conditional), caption: caption(caption_text, page_header, conditional),
legend: legend(question, page_header, conditional), legend: legend(question, page_header, conditional),
hint: { text: question.hint_text&.html_safe } do %> hint: { text: question.hint_text&.html_safe } do %>
<% after_divider = false %>
<% question.answer_options.map do |key, val| %> <% question.answer_options.map do |key, val| %>
<% if key.starts_with?("divider") %> <% if key.starts_with?("divider") %>
<% after_divider = true %>
<%= f.govuk_check_box_divider %> <%= f.govuk_check_box_divider %>
<% else %> <% else %>
<%= f.govuk_check_box question.id, key, <%= f.govuk_check_box question.id, key,
label: { text: val }, label: { text: val },
checked: @case_log[key] == "Yes", checked: @case_log[key] == "Yes",
exclusive: after_divider,
**stimulus_html_attributes(question) **stimulus_html_attributes(question)
%> %>
<% end %> <% end %>

41
spec/features/form/checkboxes_spec.rb

@ -0,0 +1,41 @@
require "rails_helper"
require_relative "helpers"
require_relative "../../request_helper"
RSpec.describe "Checkboxes" do
include Helpers
let(:user) { FactoryBot.create(:user) }
let(:case_log) do
FactoryBot.create(
:case_log,
:in_progress,
owning_organisation: user.organisation,
managing_organisation: user.organisation,
)
end
let(:id) { case_log.id }
before do
RequestHelper.stub_http_requests
sign_in user
end
context "when exclusive checkbox is selected", js: true do
it "deselects all other checkboxes" do
visit("/logs/#{id}/accessibility-requirements")
page.check("case-log-accessibility-requirements-housingneeds-a-field", allow_label_click: true)
click_button("Save and continue")
case_log.reload
expect(case_log["housingneeds_a"]).to eq("Yes")
visit("/logs/#{id}/accessibility-requirements")
page.check("case-log-accessibility-requirements-housingneeds-h-field", allow_label_click: true)
click_button("Save and continue")
case_log.reload
expect(case_log["housingneeds_a"]).to eq("No")
expect(case_log["housingneeds_h"]).to eq("Yes")
end
end
end
Loading…
Cancel
Save