Skip to content

Commit

Permalink
Add include_hidden option to check box collection
Browse files Browse the repository at this point in the history
This option defaults to true and allows developers to control whether or
not a hidden field is injected into their collection of check boxes.

Refs #238
  • Loading branch information
peteryates committed Feb 16, 2021
1 parent 5e446fa commit 91db189
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
4 changes: 3 additions & 1 deletion lib/govuk_design_system_formbuilder/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ def govuk_radio_divider(text = config.default_radio_divider_text)
# @param form_group [Hash] configures the form group
# @option form_group classes [Array,String] sets the form group's classes
# @option form_group kwargs [Hash] additional attributes added to the form group
# @param include_hidden [Boolean] controls whether a hidden field is inserted to allow for empty submissions
# @param block [Block] any HTML passed in will be injected into the fieldset, after the hint and before the checkboxes
# @return [ActiveSupport::SafeBuffer] HTML output
#
Expand Down Expand Up @@ -682,7 +683,7 @@ def govuk_radio_divider(text = config.default_radio_divider_text)
# :name,
# legend: -> { tag.h3('What kind of sandwich do you want?') }
#
def govuk_collection_check_boxes(attribute_name, collection, value_method, text_method, hint_method = nil, hint: {}, legend: {}, caption: {}, small: false, classes: nil, form_group: {}, &block)
def govuk_collection_check_boxes(attribute_name, collection, value_method, text_method, hint_method = nil, hint: {}, legend: {}, caption: {}, small: false, classes: nil, form_group: {}, include_hidden: true, &block)
Elements::CheckBoxes::Collection.new(
self,
object_name,
Expand All @@ -697,6 +698,7 @@ def govuk_collection_check_boxes(attribute_name, collection, value_method, text_
small: small,
classes: classes,
form_group: form_group,
include_hidden: include_hidden,
&block
).html
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ class Collection < Base
include Traits::Hint
include Traits::Supplemental

def initialize(builder, object_name, attribute_name, collection, value_method:, text_method:, hint:, legend:, caption:, small:, classes:, form_group:, hint_method: nil, &block)
def initialize(builder, object_name, attribute_name, collection, value_method:, text_method:, hint:, legend:, caption:, small:, classes:, form_group:, include_hidden:, hint_method: nil, &block)
super(builder, object_name, attribute_name, &block)

@collection = collection
@value_method = value_method
@text_method = text_method
@hint_method = hint_method
@small = small
@legend = legend
@caption = caption
@hint = hint
@classes = classes
@form_group = form_group
@collection = collection
@value_method = value_method
@text_method = text_method
@hint_method = hint_method
@small = small
@legend = legend
@caption = caption
@hint = hint
@classes = classes
@form_group = form_group
@include_hidden = include_hidden
end

def html
Expand Down Expand Up @@ -56,7 +57,7 @@ def check_boxes
def collection
link_errors = has_errors?

@builder.collection_check_boxes(@attribute_name, @collection, @value_method, @text_method) do |check_box|
@builder.collection_check_boxes(@attribute_name, @collection, @value_method, @text_method, include_hidden: @include_hidden) do |check_box|
Elements::CheckBoxes::CollectionCheckBox.new(
@builder,
@object_name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@
end
end

specify 'a hidden field is present by default' do
expect(subject).to have_tag('input', with: { type: 'hidden', name: "#{object_name}[#{attribute}][]" })
end

context 'check box size' do
context 'when small is specified in the options' do
subject { builder.send(*args, small: true) }
Expand Down Expand Up @@ -156,6 +160,14 @@
it_behaves_like 'a collection field that supports setting the value via a proc' do
let(:attribute) { :favourite_colour }
end

context 'suppressing the hidden field' do
subject { builder.send(*args, include_hidden: false) }

specify "the hidden field should be present within the fieldset" do
expect(subject).not_to have_tag('input', with: { type: 'hidden' })
end
end
end
end
end

0 comments on commit 91db189

Please sign in to comment.