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
15 changes: 12 additions & 3 deletions lib/bootstrap_form/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,20 @@ def form_group_builder(method, options, html_options = nil)
end

unless options.delete(:skip_label)
label_class = hide_class if options.delete(:hide_label)
if options[:label].is_a?(Hash)
label_text = options[:label].delete(:text)
label_class = options[:label].delete(:class)
options.delete(:label)
end
label_class ||= options.delete(:label_class)
label_class = hide_class if options.delete(:hide_label)

if options[:label].is_a?(String)
label_text ||= options.delete(:label)
end

form_group_options.reverse_merge!(label: {
text: options.delete(:label),
form_group_options.merge!(label: {
text: label_text,
class: label_class
})
end
Expand Down
19 changes: 17 additions & 2 deletions test/bootstrap_form_group_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,36 @@ def setup
setup_test_fixture
end

test "changing the label text" do
test "changing the label text via the label option parameter" do
expected = %{<div class="form-group"><label class="control-label required" for="user_email">Email Address</label><input class="form-control" id="user_email" name="user[email]" type="text" value="steve@example.com" /></div>}
assert_equal expected, @builder.text_field(:email, label: 'Email Address')
end

test "changing the label text via the html_options label hash" do
expected = %{<div class="form-group"><label class="control-label required" for="user_email">Email Address</label><input class="form-control" id="user_email" name="user[email]" type="text" value="steve@example.com" /></div>}
assert_equal expected, @builder.text_field(:email, label: {text: 'Email Address'})
end

test "hiding a label" do
expected = %{<div class="form-group"><label class="sr-only control-label required" for="user_email">Email</label><input class="form-control" id="user_email" name="user[email]" type="text" value="steve@example.com" /></div>}
assert_equal expected, @builder.text_field(:email, hide_label: true)
end

test "adding a custom label class" do
test "adding a custom label class via the label_class parameter" do
expected = %{<div class="form-group"><label class="btn control-label required" for="user_email">Email</label><input class="form-control" id="user_email" name="user[email]" type="text" value="steve@example.com" /></div>}
assert_equal expected, @builder.text_field(:email, label_class: 'btn')
end

test "adding a custom label class via the html_options label hash" do
expected = %{<div class="form-group"><label class="btn control-label required" for="user_email">Email</label><input class="form-control" id="user_email" name="user[email]" type="text" value="steve@example.com" /></div>}
assert_equal expected, @builder.text_field(:email, label: {class: 'btn'})
end

test "adding a custom label and changing the label text via the html_options label hash" do
expected = %{<div class="form-group"><label class="btn control-label required" for="user_email">Email Address</label><input class="form-control" id="user_email" name="user[email]" type="text" value="steve@example.com" /></div>}
assert_equal expected, @builder.text_field(:email, label: {class: 'btn', text: "Email Address"})
end

test "skipping a label" do
expected = %{<div class="form-group"><input class="form-control" id="user_email" name="user[email]" type="text" value="steve@example.com" /></div>}
assert_equal expected, @builder.text_field(:email, skip_label: true)
Expand Down