Skip to content

Commit f4d76b5

Browse files
authored
Document how to generate empty label. Issue #457. (#468)
1 parent fbce97a commit f4d76b5

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,21 @@ Which outputs:
659659
bootstrap_form follows standard rails conventions so it's i18n-ready. See more
660660
here: http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models
661661

662+
## Other Tips and Edge Cases
663+
By their very nature, forms are extremely diverse. It would be extremely difficult to provide a gem that could handle every need. Here are some tips for handling edge cases.
664+
665+
### Empty But Visible Labels
666+
Some third party plug-ins require an empty but visible label on an input control. The `hide_label` option generates a label that won't appear on the screen, but it's considered invisible and therefore doesn't work with such a plug-in. An empty label (e.g. `""`) causes the underlying Rails helper to generate a label based on the field's attribute's name.
667+
668+
The solution is to use a zero-width character for the label, or some other "empty" HTML. For example:
669+
```
670+
label: "​".html_safe
671+
```
672+
or
673+
```
674+
label: "<span></span>".html_safe
675+
```
676+
662677
## Code Triage page
663678
664679
http://www.codetriage.com/potenza/bootstrap_form

test/bootstrap_checkbox_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ class BootstrapCheckboxTest < ActionView::TestCase
1818
assert_equivalent_xml expected, @builder.check_box(:terms, label: 'I agree to the terms')
1919
end
2020

21+
test "check_box empty label" do
22+
expected = <<-HTML.strip_heredoc
23+
<div class="form-check">
24+
<input name="user[terms]" type="hidden" value="0" />
25+
<input class="form-check-input" id="user_terms" name="user[terms]" type="checkbox" value="1" />
26+
<label class="form-check-label" for="user_terms">&#8203;</label>
27+
</div>
28+
HTML
29+
# &#8203; is a zero-width space.
30+
assert_equivalent_xml expected, @builder.check_box(:terms, label: "&#8203;".html_safe)
31+
end
32+
2133
test "disabled check_box has proper wrapper classes" do
2234
expected = <<-HTML.strip_heredoc
2335
<div class="form-check">

test/bootstrap_radio_button_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ class BootstrapRadioButtonTest < ActionView::TestCase
1717
assert_equivalent_xml expected, @builder.radio_button(:misc, '1', label: 'This is a radio button')
1818
end
1919

20+
test "radio_button no label" do
21+
expected = <<-HTML.strip_heredoc
22+
<div class="form-check">
23+
<input class="form-check-input" id="user_misc_1" name="user[misc]" type="radio" value="1" />
24+
<label class="form-check-label" for="user_misc_1">&#8203;</label>
25+
</div>
26+
HTML
27+
# &#8203; is a zero-width space.
28+
assert_equivalent_xml expected, @builder.radio_button(:misc, '1', label: '&#8203;'.html_safe)
29+
end
30+
2031
test "radio_button with error is wrapped correctly" do
2132
@user.errors.add(:misc, "error for test")
2233
expected = <<-HTML.strip_heredoc

0 commit comments

Comments
 (0)