diff --git a/CHANGELOG.md b/CHANGELOG.md index cf461f890..9b6a8f437 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,9 @@ In addition to these necessary markup changes, the bootstrap_form API itself has ### Bugfixes * Your contribution here! +* [#357](https://github.com/bootstrap-ruby/bootstrap_form/pull/357) if provided, + use html option `id` to specify `for` attribute on label + [@duleorlovic](https://github.com/duleorlovic) ## [2.7.0][] (2017-04-21) diff --git a/lib/bootstrap_form/form_builder.rb b/lib/bootstrap_form/form_builder.rb index 2b22da707..7c1362643 100644 --- a/lib/bootstrap_form/form_builder.rb +++ b/lib/bootstrap_form/form_builder.rb @@ -144,10 +144,10 @@ def check_box_with_bootstrap(name, options = {}, checked_value = "1", unchecked_ disabled_class = " disabled" if options[:disabled] if options[:inline] label_class = " #{label_class}" if label_class - label(label_name, html, class: "form-check-inline#{disabled_class}#{label_class}") + label(label_name, html, { class: "form-check-inline#{disabled_class}#{label_class}" }.merge(options[:id].present? ? { for: options[:id] } : {})) else content_tag(:div, class: "form-check#{disabled_class}") do - label(label_name, html, class: ["form-check-label", label_class].compact.join(" ")) + label(label_name, html, { class: ["form-check-label", label_class].compact.join(" ") }.merge(options[:id].present? ? { for: options[:id] } : {})) end end end @@ -179,10 +179,10 @@ def radio_button_with_bootstrap(name, value, *args) else if options[:inline] label_class = " #{label_class}" if label_class - label(name, html, class: "radio-inline#{disabled_class}#{label_class}", value: value) + label(name, html, { class: "radio-inline#{disabled_class}#{label_class}", value: value }.merge(options[:id].present? ? { for: options[:id] } : {})) else content_tag(:div, class: "radio#{disabled_class}") do - label(name, html, value: value, class: label_class) + label(name, html, { value: value, class: label_class }.merge(options[:id].present? ? { for: options[:id] } : {})) end end end @@ -368,7 +368,7 @@ def form_group_builder(method, options, html_options = nil) text: label_text, class: label_class, skip_required: options.delete(:skip_required) - } + }.merge(css_options[:id].present? ? { for: css_options[:id] } : {}) end form_group(method, form_group_options) do diff --git a/test/bootstrap_checkbox_test.rb b/test/bootstrap_checkbox_test.rb index 2181ad970..49eb53eb3 100644 --- a/test/bootstrap_checkbox_test.rb +++ b/test/bootstrap_checkbox_test.rb @@ -70,6 +70,19 @@ class BootstrapCheckboxTest < ActionView::TestCase assert_equivalent_xml expected, @builder.check_box(:terms, label_class: 'btn') end + test "check_box 'id' attribute is used to specify label 'for' attribute" do + expected = <<-HTML.strip_heredoc +
+ +
+ HTML + assert_equivalent_xml expected, @builder.check_box(:terms, id: 'custom_id') + end + test "check_box responds to checked_value and unchecked_value arguments" do expected = <<-HTML.strip_heredoc
diff --git a/test/bootstrap_fields_test.rb b/test/bootstrap_fields_test.rb index f143b15e4..60c61fff7 100644 --- a/test/bootstrap_fields_test.rb +++ b/test/bootstrap_fields_test.rb @@ -176,6 +176,16 @@ class BootstrapFieldsTest < ActionView::TestCase assert_equivalent_xml expected, @builder.text_field(:email) end + test "field 'id' attribute is used to specify label 'for' attribute" do + expected = <<-HTML.strip_heredoc +
+ + +
+ HTML + assert_equivalent_xml expected, @builder.text_field(:email, id: :custom_id) + end + test "time fields are wrapped correctly" do expected = <<-HTML.strip_heredoc
diff --git a/test/bootstrap_radio_button_test.rb b/test/bootstrap_radio_button_test.rb index c19cdd47f..83e6b2755 100644 --- a/test/bootstrap_radio_button_test.rb +++ b/test/bootstrap_radio_button_test.rb @@ -41,6 +41,18 @@ class BootstrapRadioButtonTest < ActionView::TestCase assert_equivalent_xml expected, @builder.radio_button(:misc, '1', label: 'This is a radio button', label_class: 'btn') end + test "radio_button 'id' attribute is used to specify label 'for' attribute" do + expected = <<-HTML.strip_heredoc +
+ +
+ HTML + assert_equivalent_xml expected, @builder.radio_button(:misc, '1', label: 'This is a radio button', id: 'custom_id') + end + test "radio_button inline label is set correctly" do expected = <<-HTML.strip_heredoc