Skip to content

Commit 4b09898

Browse files
committed
Fixing field spacing on horizontal forms when no label exists
1 parent 0f72cbf commit 4b09898

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

lib/bootstrap_form/form_builder.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class FormBuilder < ActionView::Helpers::FormBuilder
99

1010
def initialize(object_name, object, template, options, proc=nil)
1111
@style = options[:style]
12-
@left_class = options[:left] || "col-sm-2"
12+
@left_class = (options[:left] || "col-sm-2") + " control-label"
1313
@right_class = options[:right] || "col-sm-10"
1414
super
1515
end
@@ -78,10 +78,14 @@ def form_group(name = nil, options = {}, &block)
7878

7979
label_options = options.delete(:label)
8080
label_html = if label_options
81-
label_options[:class] = "#{@left_class} control-label".lstrip if @style == :horizontal
81+
label_options[:class] = "#{label_options[:class]} #{@left_class}".lstrip if @style == :horizontal
8282
label(name, label_options[:text], label_options.except(:text))
8383
end || ""
8484

85+
if label_html.empty? && @style == :horizontal
86+
label_html = content_tag(:label, "", class: @left_class)
87+
end
88+
8589
html = capture(&block)
8690

8791
help_text = options.delete(:help)

test/bootstrap_form_test.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,22 @@ def setup
192192

193193
test "form_group creates a valid structure and allows arbitrary html to be added via a block" do
194194
output = @horizontal_builder.form_group :nil, label: { text: 'Foo' } do
195-
content_tag :p, "Bar", class: "form-control-static"
195+
%{<p class="form-control-static">Bar</p>}.html_safe
196196
end
197197

198198
expected = %{<div class="form-group"><label class="col-sm-2 control-label" for="user_nil">Foo</label><div class="col-sm-10"><p class="form-control-static">Bar</p></div></div>}
199199
assert_equal expected, output
200200
end
201201

202+
test "form_group adds a spacer when no label exists for a horizontal form" do
203+
output = @horizontal_builder.form_group do
204+
content_tag :p, "Bar", class: "form-control-static"
205+
end
206+
207+
expected = %{<div class="form-group"><label class="col-sm-2 control-label"></label><div class="col-sm-10"><p class="form-control-static">Bar</p></div></div>}
208+
assert_equal expected, output
209+
end
210+
202211
test "control_group renders the options for div.control_group" do
203212
skip
204213
output = @builder.control_group nil, id: 'foo' do

0 commit comments

Comments
 (0)