Skip to content

Commit 6461e52

Browse files
committed
Test passing. Collections don't respect id:
1 parent df38405 commit 6461e52

File tree

11 files changed

+25
-13
lines changed

11 files changed

+25
-13
lines changed

lib/bootstrap_form/components/labels.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def generate_label(id, name, options, custom_label_col, group_layout)
1818

1919
options[:class] = label_classes(name, options, custom_label_col, group_layout)
2020
options.delete(:class) if options[:class].none?
21-
options[:id] = field_id(name, :feedback) if error?(name) && label_errors
21+
options[:id] = id.present? ? "#{id}_feedback" : field_id(name, :feedback) if error?(name) && label_errors
2222

2323
label(name, label_text(name, options), options.except(:text))
2424
end

lib/bootstrap_form/components/validation.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,15 @@ def inline_error?(name)
6161
error?(name) && inline_errors
6262
end
6363

64-
def generate_error(name)
64+
def generate_error(name, id)
6565
return unless inline_error?(name)
6666

6767
help_text = get_error_messages(name)
6868
help_klass = "invalid-feedback"
6969
help_tag = :div
70+
id = id.present? ? "#{id}_feedback" : field_id(name, :feedback)
7071

71-
content_tag(help_tag, help_text, class: help_klass, id: field_id(name, :feedback))
72+
content_tag(help_tag, help_text, class: help_klass, id:)
7273
end
7374

7475
def get_error_messages(name)

lib/bootstrap_form/form_group.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def form_group_content_tag(name, field_name, without_field_name, options, html_o
2323
html_class = control_specific_class(field_name)
2424
html_class = "#{html_class} col-auto g-3" if @layout == :horizontal && options[:skip_inline].blank?
2525
tag.div(class: html_class) do
26-
input_with_error(name) do
26+
input_with_error(name, options[:id]) do
2727
send(without_field_name, name, options, html_options)
2828
end
2929
end

lib/bootstrap_form/form_group_builder.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ def form_group_css_options(method, html_options, options)
9494
css_options[:class] = safe_join([control_classes, css_options[:class]].compact, " ")
9595
if error?(method)
9696
css_options[:class] << " is-invalid"
97-
css_options[:aria] = { labelledby: field_id(method, :feedback) }
97+
labelledby = options[:id].present? ? "#{options[:id]}_feedback" : field_id(method, :feedback)
98+
css_options[:aria] = { labelledby: }
9899
end
99100
css_options[:placeholder] = form_group_placeholder(options, method) if options[:label_as_placeholder]
100101
css_options

lib/bootstrap_form/helpers/bootstrap.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,21 @@ def custom_control(*args, &)
6666
end
6767

6868
def prepend_and_append_input(name, options, &)
69+
id = options[:id]
6970
options = options.extract!(:prepend, :append, :input_group_class).compact
7071

7172
input = capture(&) || ActiveSupport::SafeBuffer.new
7273

7374
input = attach_input(options, :prepend) + input + attach_input(options, :append)
74-
input << generate_error(name)
75+
input << generate_error(name, id)
7576
options.present? &&
7677
input = tag.div(input, class: ["input-group", options[:input_group_class]].compact)
7778
input
7879
end
7980

80-
def input_with_error(name, &)
81+
def input_with_error(name, id, &)
8182
input = capture(&)
82-
input << generate_error(name)
83+
input << generate_error(name, id)
8384
end
8485

8586
def input_group_content(content)

lib/bootstrap_form/inputs/base.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def bootstrap_field(field_name)
2424

2525
def bootstrap_select_group(field_name)
2626
define_method(:"#{field_name}_with_bootstrap") do |name, options={}, html_options={}|
27+
options.delete(:id)
2728
html_options = html_options.reverse_merge(control_class: "form-select")
2829
form_group_builder(name, options, html_options) do
2930
form_group_content_tag(name, field_name, "#{field_name}_without_bootstrap", options, html_options)

lib/bootstrap_form/inputs/check_box.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def check_box_with_bootstrap(name, options={}, checked_value="1", unchecked_valu
1313
content = tag.div(class: check_box_wrapper_class(options), **options[:wrapper].to_h.except(:class)) do
1414
html = check_box_without_bootstrap(name, check_box_options(name, options), checked_value, unchecked_value)
1515
html << check_box_label(name, options, checked_value, &block) unless options[:skip_label]
16-
html << generate_error(name) if options[:error_message]
16+
html << generate_error(name, options[:id]) if options[:error_message]
1717
html
1818
end
1919
wrapper(content, options)
@@ -41,7 +41,10 @@ def check_box_options(name, options)
4141
:inline, :label, :label_class, :label_col, :layout, :skip_label,
4242
:switch, :wrapper, :wrapper_class)
4343
check_box_options[:class] = check_box_classes(name, options)
44-
check_box_options[:aria] = { labelledby: field_id(name, :feedback) } if error?(name)
44+
if error?(name)
45+
labelledby = options[:id].present? ? "#{options[:id]}_feedback" : field_id(name, :feedback)
46+
check_box_options[:aria] = { labelledby: }
47+
end
4548
check_box_options.merge!(required_field_options(options, name))
4649
end
4750

lib/bootstrap_form/inputs/collection_check_boxes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module CollectionCheckBoxes
99

1010
included do
1111
def collection_check_boxes_with_bootstrap(*args)
12+
args[4]&.delete(:id)
1213
html = inputs_collection(*args) do |name, value, options|
1314
options[:multiple] = true
1415
check_box(name, options, value, nil)

lib/bootstrap_form/inputs/collection_radio_buttons.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module CollectionRadioButtons
99

1010
included do
1111
def collection_radio_buttons_with_bootstrap(*args)
12+
args[4]&.delete(:id)
1213
inputs_collection(*args) do |name, value, options|
1314
radio_button(name, value, options)
1415
end

lib/bootstrap_form/inputs/radio_button.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def radio_button_with_bootstrap(name, value, *args)
1414
tag.div(**wrapper_attributes) do
1515
html = radio_button_without_bootstrap(name, value, radio_button_options(name, options))
1616
html << radio_button_label(name, value, options) unless options[:skip_label]
17-
html << generate_error(name) if options[:error_message]
17+
html << generate_error(name, options[:id]) if options[:error_message]
1818
html
1919
end
2020
end
@@ -28,7 +28,10 @@ def radio_button_options(name, options)
2828
radio_button_options = options.except(:class, :label, :label_class, :error_message, :help,
2929
:inline, :hide_label, :skip_label, :wrapper, :wrapper_class)
3030
radio_button_options[:class] = radio_button_classes(name, options)
31-
radio_button_options[:aria] = { labelledby: field_id(name, :feedback) } if error?(name)
31+
if error?(name)
32+
labelledby = options[:id].present? ? "#{options[:id]}_feedback" : field_id(name, :feedback)
33+
radio_button_options[:aria] = { labelledby: }
34+
end
3235
radio_button_options.merge!(required_field_options(options, name))
3336
end
3437

0 commit comments

Comments
 (0)