diff --git a/lib/bootstrap_form/form_builder.rb b/lib/bootstrap_form/form_builder.rb index e127c7bb3..b6ab74fb6 100644 --- a/lib/bootstrap_form/form_builder.rb +++ b/lib/bootstrap_form/form_builder.rb @@ -134,10 +134,10 @@ def check_box_with_bootstrap(name, options = {}, checked_value = "1", unchecked_ if options[:inline] label_class = " #{label_class}" if label_class - label(label_name, html, class: "checkbox-inline#{disabled_class}#{label_class}") + label(label_name, html, { class: "checkbox-inline#{disabled_class}#{label_class}" }.merge(options[:id].present? ? { for: options[:id] } : {})) else content_tag(:div, class: "checkbox#{disabled_class}") do - label(label_name, html, class: label_class) + label(label_name, html, { class: label_class }.merge(options[:id].present? ? { for: options[:id] } : {})) end end end @@ -155,10 +155,10 @@ def radio_button_with_bootstrap(name, value, *args) 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 @@ -350,11 +350,11 @@ def form_group_builder(method, options, html_options = nil) label_text ||= options.delete(:label) end - form_group_options.merge!(label: { + form_group_options[:label] = { 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 @@ -369,6 +369,10 @@ def convert_form_tag_options(method, options = {}) end def generate_label(id, name, options, custom_label_col, group_layout) + # id is the caller's options[:id] at the only place this method is called. + # The options argument is a small subset of the options that might have + # been passed to generate_label's caller, and definitely doesn't include + # :id. options[:for] = id if acts_like_form_tag classes = [options[:class], label_class] classes << (custom_label_col || label_col) if get_group_layout(group_layout) == :horizontal diff --git a/test/bootstrap_checkbox_test.rb b/test/bootstrap_checkbox_test.rb index 0d2832690..3770b216c 100644 --- a/test/bootstrap_checkbox_test.rb +++ b/test/bootstrap_checkbox_test.rb @@ -32,6 +32,11 @@ def setup 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 = %{
} + 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 = %{} assert_equivalent_xml expected, @builder.check_box(:terms, {label: 'I agree to the terms'}, 'yes', 'no') diff --git a/test/bootstrap_fields_test.rb b/test/bootstrap_fields_test.rb index 3eaec8ece..03ff5ea30 100644 --- a/test/bootstrap_fields_test.rb +++ b/test/bootstrap_fields_test.rb @@ -83,6 +83,11 @@ def setup assert_equivalent_xml expected, @builder.text_field(:email) end + test "field 'id' attribute is used to specify label 'for' attribute" do + expected = %{} + assert_equivalent_xml expected, @builder.text_field(:email, id: :custom_id) + end + test "time fields are wrapped correctly" do expected = %{} assert_equivalent_xml expected, @builder.time_field(:misc) diff --git a/test/bootstrap_radio_button_test.rb b/test/bootstrap_radio_button_test.rb index b420c1e01..33d47cc28 100644 --- a/test/bootstrap_radio_button_test.rb +++ b/test/bootstrap_radio_button_test.rb @@ -22,6 +22,18 @@ def setup 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 +