Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<div class="form-group has-warning">
<div class="form-group has-warning" data-foo="bar">
<label class="control-label" for="user_name">Id</label>
<input class="form-control" id="user_name" name="user[name]" type="text">
</div>
```

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
Expand Down
20 changes: 19 additions & 1 deletion lib/bootstrap_form/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions test/bootstrap_form_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -786,4 +786,9 @@ def setup
expected = %{<div class="form-group"><div class="col-sm-10 col-sm-offset-2">Hallo</div></div><div class="form-group"><label class="control-label col-sm-2" for="user_email">Email</label><div class="col-sm-10"><input class="form-control" id="user_email" name="user[email]" type="text" value="steve@example.com" /></div></div>}
assert_equal expected, output
end

test "adds data-attributes (or any other options) to wrapper" do
expected = %{<div class="form-group" data-foo="bar"><label class="control-label" for="user_misc">Misc</label><input class="form-control" id="user_misc" name="user[misc]" type="search" /></div>}
assert_equal expected, @builder.search_field(:misc, wrapper: { data: { foo: 'bar' } })
end
end