diff --git a/README.md b/README.md index b8b2b4ee3..b113ff16e 100644 --- a/README.md +++ b/README.md @@ -189,24 +189,26 @@ You can also prepend and append buttons. Note: The buttons must contain the <%= f.text_field :search, append: link_to("Go", "#", class: "btn btn-default") %> ``` -### Additional Form Group Classes +### Additional Form Group Attributes -If you want to add an additional css class to the form group div, you can use -the `wrapper_class: 'additional-class'` option. +If you want to add an additional css class or any other attribute to the form group div, you can use +the `wrapper: { class: 'additional-class', data: { foo: 'bar' } }` option. ```erb -<%= f.text_field :name, wrapper_class: 'has-warning' %> +<%= f.text_field :name, wrapper: { class: 'has-warning', data: { foo: 'bar' } } %> ``` Which produces the following output: ```erb -
+
``` +You still can use `wrapper_class` option to set only a css class. This is just a short form of `wrapper: { class: 'additional-class' }`. + ### Checkboxes and Radios Checkboxes and radios should be placed inside of a `form_group` to render diff --git a/lib/bootstrap_form/form_builder.rb b/lib/bootstrap_form/form_builder.rb index 0877d7d58..c5bf0c23d 100644 --- a/lib/bootstrap_form/form_builder.rb +++ b/lib/bootstrap_form/form_builder.rb @@ -256,13 +256,31 @@ def form_group_builder(method, options, html_options = nil) label = options.delete(:label) label_class = hide_class if options.delete(:hide_label) wrapper_class = options.delete(:wrapper_class) + wrapper_options = options.delete(:wrapper) help = options.delete(:help) icon = options.delete(:icon) label_col = options.delete(:label_col) control_col = options.delete(:control_col) layout = get_group_layout(options.delete(:layout)) + form_group_options = { + id: options[:id], + label: { + text: label, + class: label_class + }, + help: help, + icon: icon, + label_col: label_col, + control_col: control_col, + layout: layout, + class: wrapper_class + } + + if wrapper_options.is_a?(Hash) + form_group_options.reverse_merge!(wrapper_options) + end - form_group(method, id: options[:id], label: { text: label, class: label_class }, help: help, icon: icon, label_col: label_col, control_col: control_col, layout: layout, class: wrapper_class) do + form_group(method, form_group_options) do yield end end diff --git a/test/bootstrap_form_test.rb b/test/bootstrap_form_test.rb index 300549ec1..52e8bf051 100644 --- a/test/bootstrap_form_test.rb +++ b/test/bootstrap_form_test.rb @@ -786,4 +786,9 @@ def setup expected = %{
Hallo
} assert_equal expected, output end + + test "adds data-attributes (or any other options) to wrapper" do + expected = %{
} + assert_equal expected, @builder.search_field(:misc, wrapper: { data: { foo: 'bar' } }) + end end