Skip to content

Commit 61fc37a

Browse files
committed
Added examples for check box /radio button collections with inline errors to the README.md
Fixes #655
1 parent 8ff07f8 commit 61fc37a

40 files changed

+46
-9
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,6 +1423,8 @@ error will be displayed below the field. Here's an example:
14231423
```erb
14241424
<%= bootstrap_form_for(@user_with_error) do |f| %>
14251425
<%= f.email_field :email %>
1426+
<%= f.collection_radio_buttons :misc, Skill.all, :id, :name %>
1427+
<%= f.collection_check_boxes :preferences, [[1, 'Good'], [2, 'Bad']], :first, :second %>
14261428
<%= f.fields_for :address do |af| %>
14271429
<%= af.text_field :street %>
14281430
<% end %>
@@ -1438,6 +1440,31 @@ Generated HTML:
14381440
<input class="form-control is-invalid" id="user_email" name="user[email]" required="required" type="email" value="steve.example.com">
14391441
<div class="invalid-feedback">is invalid</div>
14401442
</div>
1443+
<div class="mb-3">
1444+
<label class="form-label" for="user_misc">Misc</label>
1445+
<div class="form-check">
1446+
<input checked class="form-check-input is-invalid" id="user_misc_1" name="user[misc]" type="radio" value="1">
1447+
<label class="form-check-label" for="user_misc_1">Mind reading</label>
1448+
</div>
1449+
<div class="form-check">
1450+
<input class="form-check-input is-invalid" id="user_misc_2" name="user[misc]" type="radio" value="2">
1451+
<label class="form-check-label" for="user_misc_2">Farming</label>
1452+
<div class="invalid-feedback">is invalid</div>
1453+
</div>
1454+
</div>
1455+
<input autocomplete="off" id="user_preferences" name="user[preferences][]" type="hidden" value="">
1456+
<div class="mb-3">
1457+
<label class="form-label" for="user_preferences">Preferences</label>
1458+
<div class="form-check">
1459+
<input checked class="form-check-input is-invalid" id="user_preferences_1" name="user[preferences][]" type="checkbox" value="1">
1460+
<label class="form-check-label" for="user_preferences_1">Good</label>
1461+
</div>
1462+
<div class="form-check">
1463+
<input class="form-check-input is-invalid" id="user_preferences_2" name="user[preferences][]" type="checkbox" value="2">
1464+
<label class="form-check-label" for="user_preferences_2">Bad</label>
1465+
<div class="invalid-feedback">is invalid</div>
1466+
</div>
1467+
</div>
14411468
<div class="mb-3">
14421469
<label class="form-label" for="user_address_attributes_street">Street</label>
14431470
<input class="form-control is-invalid" id="user_address_attributes_street" name="user[address_attributes][street]" type="text" value="Bar">
@@ -1508,6 +1535,7 @@ Which outputs:
15081535
<ul class="rails-bootstrap-forms-error-summary">
15091536
<li>Email is invalid</li>
15101537
<li>Misc is invalid</li>
1538+
<li>Preferences is invalid</li>
15111539
</ul>
15121540
</div>
15131541
</form>
@@ -1546,6 +1574,7 @@ Which outputs:
15461574
<ul class="rails-bootstrap-forms-error-summary">
15471575
<li>Email is invalid</li>
15481576
<li>Misc is invalid</li>
1577+
<li>Preferences is invalid</li>
15491578
</ul>
15501579
</form>
15511580
```
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
// @import "actiontext";
22

33
label.required:after {
4-
content:" *";
4+
content: " *";
55
color: red;
66
}
7+
8+
.fragment {
9+
@extend .border;
10+
@extend .p-3;
11+
}
12+
13+
form, .alert, .fragment {
14+
> :last-child {
15+
margin-bottom: 0 !important;
16+
}
17+
}

demo/app/controllers/bootstrap_controller.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def form
66
def fragment
77
@erb = params[:erb]
88

9-
@erb.prepend '<div class="p-3 border">'
9+
@erb.prepend '<div class="fragment">'
1010
@erb << "</div>"
1111
load_models
1212
render inline: @erb, layout: "application" # rubocop: disable Rails/RenderInline
@@ -20,9 +20,10 @@ def load_models
2020
@address_with_error.errors.add(:street)
2121
@collection = [@address, @address_with_error]
2222
@user = User.new email: "[email protected]"
23-
@user_with_error = User.new email: "steve.example.com", address: @address_with_error
23+
@user_with_error = User.new email: "steve.example.com", address: @address_with_error, misc: 1, preferences: [1]
2424
@user_with_error.errors.add(:email)
2525
@user_with_error.errors.add(:misc)
26+
@user_with_error.errors.add(:preferences)
2627
@users = [@user]
2728
end
2829
end

demo/app/models/user.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
class User < ApplicationRecord
22
attr_accessor :remember_me
33

4-
if Rails::VERSION::STRING >= "6.1"
5-
serialize :preferences, coder: JSON
6-
else
7-
serialize :preferences, JSON
8-
end
4+
serialize :preferences, coder: JSON
95

106
validates :email, presence: true, length: { minimum: 5 }
117
validates :terms, acceptance: { accept: true }
6.03 KB
Loading
-54 Bytes
Loading
-54 Bytes
Loading
-58 Bytes
Loading
-50 Bytes
Loading
-62 Bytes
Loading

0 commit comments

Comments
 (0)