bootstrap_form 4.1.0 → 4.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +18 -0
- data/.rubocop.yml +3 -2
- data/.travis.yml +7 -1
- data/CHANGELOG.md +15 -1
- data/CONTRIBUTING.md +11 -0
- data/Dangerfile +4 -4
- data/Gemfile +7 -2
- data/OLD-README.md +795 -0
- data/README.md +150 -93
- data/Rakefile +2 -4
- data/bootstrap_form.gemspec +2 -1
- data/demo/.postcssrc.yml +3 -0
- data/demo/app/assets/config/manifest.js +2 -0
- data/demo/app/assets/stylesheets/actiontext.scss +38 -0
- data/demo/app/assets/stylesheets/application.scss +1 -0
- data/demo/app/helpers/bootstrap_helper.rb +16 -10
- data/demo/app/javascript/channels/consumer.js +6 -0
- data/demo/app/javascript/channels/index.js +5 -0
- data/demo/app/javascript/packs/application.js +11 -0
- data/demo/app/models/user.rb +2 -0
- data/demo/app/views/active_storage/blobs/_blob.html.erb +14 -0
- data/demo/app/views/bootstrap/form.html.erb +2 -1
- data/demo/app/views/layouts/application.html.erb +3 -0
- data/demo/bin/webpack +15 -0
- data/demo/bin/webpack-dev-server +15 -0
- data/demo/config/application.rb +2 -3
- data/demo/config/environments/development.rb +3 -1
- data/demo/config/environments/production.rb +48 -0
- data/demo/config/webpack/development.js +5 -0
- data/demo/config/webpack/environment.js +3 -0
- data/demo/config/webpack/production.js +5 -0
- data/demo/config/webpack/test.js +5 -0
- data/demo/config/webpacker.yml +92 -0
- data/demo/db/schema.rb +63 -18
- data/demo/package.json +13 -1
- data/demo/test/fixtures/action_text/rich_texts.yml +4 -0
- data/demo/yarn.lock +6257 -0
- data/lib/bootstrap_form.rb +34 -8
- data/lib/bootstrap_form/action_view_extensions/form_helper.rb +71 -0
- data/lib/bootstrap_form/components.rb +17 -0
- data/lib/bootstrap_form/components/hints.rb +60 -0
- data/lib/bootstrap_form/components/labels.rb +56 -0
- data/lib/bootstrap_form/components/layout.rb +39 -0
- data/lib/bootstrap_form/components/validation.rb +61 -0
- data/lib/bootstrap_form/engine.rb +10 -0
- data/lib/bootstrap_form/form_builder.rb +54 -524
- data/lib/bootstrap_form/form_group.rb +64 -0
- data/lib/bootstrap_form/form_group_builder.rb +103 -0
- data/lib/bootstrap_form/helpers.rb +9 -0
- data/lib/bootstrap_form/helpers/bootstrap.rb +39 -31
- data/lib/bootstrap_form/inputs.rb +40 -0
- data/lib/bootstrap_form/inputs/base.rb +40 -0
- data/lib/bootstrap_form/inputs/check_box.rb +89 -0
- data/lib/bootstrap_form/inputs/collection_check_boxes.rb +23 -0
- data/lib/bootstrap_form/inputs/collection_radio_buttons.rb +21 -0
- data/lib/bootstrap_form/inputs/collection_select.rb +25 -0
- data/lib/bootstrap_form/inputs/color_field.rb +14 -0
- data/lib/bootstrap_form/inputs/date_field.rb +14 -0
- data/lib/bootstrap_form/inputs/date_select.rb +14 -0
- data/lib/bootstrap_form/inputs/datetime_field.rb +14 -0
- data/lib/bootstrap_form/inputs/datetime_local_field.rb +14 -0
- data/lib/bootstrap_form/inputs/datetime_select.rb +14 -0
- data/lib/bootstrap_form/inputs/email_field.rb +14 -0
- data/lib/bootstrap_form/inputs/file_field.rb +35 -0
- data/lib/bootstrap_form/inputs/grouped_collection_select.rb +29 -0
- data/lib/bootstrap_form/inputs/inputs_collection.rb +44 -0
- data/lib/bootstrap_form/inputs/month_field.rb +14 -0
- data/lib/bootstrap_form/inputs/number_field.rb +14 -0
- data/lib/bootstrap_form/inputs/password_field.rb +14 -0
- data/lib/bootstrap_form/inputs/phone_field.rb +14 -0
- data/lib/bootstrap_form/inputs/radio_button.rb +77 -0
- data/lib/bootstrap_form/inputs/range_field.rb +14 -0
- data/lib/bootstrap_form/inputs/rich_text_area.rb +23 -0
- data/lib/bootstrap_form/inputs/search_field.rb +14 -0
- data/lib/bootstrap_form/inputs/select.rb +22 -0
- data/lib/bootstrap_form/inputs/telephone_field.rb +14 -0
- data/lib/bootstrap_form/inputs/text_area.rb +14 -0
- data/lib/bootstrap_form/inputs/text_field.rb +14 -0
- data/lib/bootstrap_form/inputs/time_field.rb +14 -0
- data/lib/bootstrap_form/inputs/time_select.rb +14 -0
- data/lib/bootstrap_form/inputs/time_zone_select.rb +22 -0
- data/lib/bootstrap_form/inputs/url_field.rb +14 -0
- data/lib/bootstrap_form/inputs/week_field.rb +14 -0
- data/lib/bootstrap_form/version.rb +1 -1
- metadata +79 -6
- data/.rubocop_todo.yml +0 -104
- data/lib/bootstrap_form/aliasing.rb +0 -35
- data/lib/bootstrap_form/helper.rb +0 -52
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
⚠️ **This documentation is for the master branch, which is not yet stable and targets Bootstrap v4.** If you are using Bootstrap v3, refer to the stable [legacy-2.7](https://github.com/bootstrap-ruby/bootstrap_form/tree/legacy-2.7) branch.
|
2
2
|
|
3
|
+
This is a new take on the `bootstrap_form` README. Please leave comments at: #520. You can go back to the traditional [README](/OLD-README.md).
|
4
|
+
|
3
5
|
---
|
4
6
|
|
5
7
|
# bootstrap_form
|
@@ -7,10 +9,26 @@
|
|
7
9
|
[![Build Status](https://travis-ci.org/bootstrap-ruby/bootstrap_form.svg?branch=master)](https://travis-ci.org/bootstrap-ruby/bootstrap_form)
|
8
10
|
[![Gem Version](https://badge.fury.io/rb/bootstrap_form.svg)](https://rubygems.org/gems/bootstrap_form)
|
9
11
|
|
10
|
-
|
11
|
-
|
12
|
+
`bootstrap_form` is a Rails form builder that makes it super easy to integrate Bootstrap v4-style forms into your Rails application. It provides form helpers that augment the Rails form helpers. `bootstrap_forms`'s form helpers generate the form field and its label and all the Bootstrap mark-up required for proper Bootstrap display. `bootstrap_form` also provides:
|
13
|
+
|
14
|
+
* [Validation error messages](#validation-and-errors) below the field they correspond to, by default. You can also put the error messages after the label, or turn off `bootstrap_form`'s validation error handling and do it yourself.
|
15
|
+
* Automatic [mark-up for the `required` attribute](#required-fields) on required fields.
|
16
|
+
* An easy way to consistently show [help](#help-text) text on fields.
|
17
|
+
* Mark-up for [Bootstrap horizontal forms](#horizontal-forms) (labels to the left of their fields, like a traditional desktop application), if that's what you want.
|
18
|
+
* Many [options](#form-helpers) to modify or augment the generated mark-up.
|
19
|
+
* A way to [escape to the Rails form helpers](#accessing-rails-form-helpers) if you need to do something that `bootstrap_form` can't do.
|
20
|
+
|
21
|
+
Some other nice things that `bootstrap_form` does for you are:
|
22
|
+
|
23
|
+
* Reduces the amount of code in your `.erb` files.
|
24
|
+
* Gets you going faster with Bootstrap, because you don't need to learn all the rules of Bootstrap form mark-up to get started.
|
25
|
+
* Reduces errors, because you're doing less typing.
|
26
|
+
* Makes it easier to see the logic of the form, because it's not mixed in with the Bootstrap mark-up.
|
27
|
+
|
28
|
+
`bootstrap_form` works like the standard Rails form helpers, and this README assumes you know how they work. You start a form with one of [`bootstrap_form_with`](#bootstrap-form-with), [`bootstrap_form_for`](#bootstrap-form-for), or [`bootstrap_form_tag`](#bootstrap-form-tag) in a view file. You get a form builder that calls the [`bootstrap_form` helpers](#form-helpers) instead of the standard Rails helpers. You use that form builder in the view file to render one or more form fields.
|
12
29
|
|
13
30
|
## Requirements
|
31
|
+
`bootstrap_form` supports currently supported versions of Rails:
|
14
32
|
|
15
33
|
* Ruby 2.2.2+
|
16
34
|
* Rails 5.0+ (Rails 5.1+ for `bootstrap_form_with`)
|
@@ -21,12 +39,12 @@ Bootstrap v4-style forms into your Rails application.
|
|
21
39
|
Add it to your Gemfile:
|
22
40
|
|
23
41
|
```ruby
|
24
|
-
gem "bootstrap_form", ">= 4.
|
42
|
+
gem "bootstrap_form", ">= 4.2.0"
|
25
43
|
```
|
26
44
|
|
27
45
|
Then:
|
28
46
|
|
29
|
-
`bundle`
|
47
|
+
`bundle install`
|
30
48
|
|
31
49
|
Then require the CSS in your `application.css` file:
|
32
50
|
|
@@ -38,7 +56,9 @@ Then require the CSS in your `application.css` file:
|
|
38
56
|
|
39
57
|
## Usage
|
40
58
|
|
41
|
-
|
59
|
+
### bootstrap_form_for
|
60
|
+
|
61
|
+
To get started, use the `bootstrap_form_for` helper in place of the Rails `form_for` helper. Here's an example:
|
42
62
|
|
43
63
|
```erb
|
44
64
|
<%= bootstrap_form_for(@user) do |f| %>
|
@@ -81,9 +101,9 @@ If your form is not backed by a model, use the `bootstrap_form_tag`. Usage of th
|
|
81
101
|
<% end %>
|
82
102
|
```
|
83
103
|
|
84
|
-
###
|
104
|
+
### bootstrap_form_with (Rails 5.1+)
|
85
105
|
|
86
|
-
Note that `form_with` in Rails 5.1 does not add IDs to form elements and labels by default, which are both important to Bootstrap markup. This
|
106
|
+
Note that `form_with` in Rails 5.1 does not add IDs to form elements and labels by default, which are both important to Bootstrap markup. This behaviour is corrected in Rails 5.2.
|
87
107
|
|
88
108
|
To get started, just use the `bootstrap_form_with` helper in place of `form_with`. Here's an example:
|
89
109
|
|
@@ -124,49 +144,50 @@ in `form_with`.
|
|
124
144
|
|
125
145
|
`form_with` has some important differences compared to `form_for` and `form_tag`, and these differences apply to `bootstrap_form_with`. A good summary of the differences can be found at: https://m.patrikonrails.com/rails-5-1s-form-with-vs-old-form-helpers-3a5f72a8c78a, or in the [Rails documentation](api.rubyonrails.org).
|
126
146
|
|
127
|
-
|
147
|
+
## Form Helpers
|
128
148
|
|
129
|
-
|
149
|
+
`bootstrap_form` provides its own version of the following Rails form helpers:
|
130
150
|
|
131
|
-
|
151
|
+
```
|
152
|
+
button email_field search_field
|
153
|
+
check_box file_field select
|
154
|
+
collection_check_boxes grouped_collection_select submit
|
155
|
+
collection_radio_buttons hidden_field (not wrapped, but supported) telephone_field
|
156
|
+
collection_select month_field text_area
|
157
|
+
color_field number_field text_field
|
158
|
+
date_field password_field time_field
|
159
|
+
date_select phone_field time_select
|
160
|
+
datetime_field radio_button time_zone_select
|
161
|
+
datetime_local_field range_field url_field
|
162
|
+
datetime_select rich_text_area (Rails 6+) week_field
|
163
|
+
```
|
164
|
+
|
165
|
+
By default, the helpers generate a `label` tag, and an `input`, `select`, or `textarea` tag, by calling the Rails `label` helper, and then the Rails helper with the same name as the `bootstrap_form` helper.
|
166
|
+
|
167
|
+
The `bootstrap_form` helpers accept the same options as the standard Rails form helpers, and pass those options through to the Rails helper. They also accept additional options, described in the following section.
|
168
|
+
|
169
|
+
## Form Helper Options
|
170
|
+
|
171
|
+
Many of the helpers accept the same options. The exceptions are:
|
132
172
|
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
* phone_field
|
152
|
-
* radio_button
|
153
|
-
* collection_radio_buttons
|
154
|
-
* range_field
|
155
|
-
* search_field
|
156
|
-
* select
|
157
|
-
* telephone_field
|
158
|
-
* text_area
|
159
|
-
* text_field
|
160
|
-
* time_field
|
161
|
-
* time_select
|
162
|
-
* time_zone_select
|
163
|
-
* url_field
|
164
|
-
* week_field
|
165
|
-
* submit
|
166
|
-
* button
|
167
|
-
|
168
|
-
These helpers accept the same options as the standard Rails form helpers, with
|
169
|
-
a few extra options:
|
173
|
+
[button](#submit-buttons),
|
174
|
+
[check_box](#checkboxes-and-radios),
|
175
|
+
[collection_check_boxes](#collections),
|
176
|
+
[collection_radio_buttons](#collections),
|
177
|
+
[collection_select](#selects),
|
178
|
+
[date_select](#date-helpers),
|
179
|
+
[datetime_select](#date-helpers),
|
180
|
+
[file_field](#file-fields),
|
181
|
+
[grouped_collection_select](#selects),
|
182
|
+
[hidden_field](#hidden-fields),
|
183
|
+
[radio_button](#checkboxes-and-radios),
|
184
|
+
[rich_text_area](#rich-text-areas-aka-trix-editor),
|
185
|
+
[select](#selects),
|
186
|
+
[submit](#submit-buttons),
|
187
|
+
[time_select](#date-helpers),
|
188
|
+
[time_zone_select](#selects)
|
189
|
+
|
190
|
+
The options for the form helpers that aren't in the exceptions list are described in the following sub-sections:
|
170
191
|
|
171
192
|
### Labels
|
172
193
|
|
@@ -195,31 +216,6 @@ Or you can add the label as input placeholder instead (this automatically hides
|
|
195
216
|
<%= f.text_field :email, label_as_placeholder: true %>
|
196
217
|
```
|
197
218
|
|
198
|
-
#### Required Fields
|
199
|
-
|
200
|
-
A label that is associated with a required field is automatically annotated with
|
201
|
-
a `required` CSS class. You are free to add any appropriate CSS to style
|
202
|
-
required fields as desired. One example would be to automatically add an
|
203
|
-
asterisk to the end of the label:
|
204
|
-
|
205
|
-
```css
|
206
|
-
label.required:after {
|
207
|
-
content:" *";
|
208
|
-
}
|
209
|
-
```
|
210
|
-
|
211
|
-
The label `required` class is determined based on the definition of a presence
|
212
|
-
validator with the associated model attribute. Presently this is one of:
|
213
|
-
ActiveRecord::Validations::PresenceValidator or
|
214
|
-
ActiveModel::Validations::PresenceValidator.
|
215
|
-
|
216
|
-
In cases where this behavior is undesirable, use the `required` option to force the class to be present or absent:
|
217
|
-
|
218
|
-
```erb
|
219
|
-
<%= f.password_field :login, label: "New Username", required: true %>
|
220
|
-
<%= f.password_field :password, label: "New Password", required: false %>
|
221
|
-
```
|
222
|
-
|
223
219
|
### Input Elements / Controls
|
224
220
|
|
225
221
|
To specify the class of the generated input tag, use the `control_class` option:
|
@@ -236,6 +232,11 @@ To add help text, use the `help` option:
|
|
236
232
|
<%= f.password_field :password, help: "Must be at least 6 characters long" %>
|
237
233
|
```
|
238
234
|
|
235
|
+
This generates:
|
236
|
+
```
|
237
|
+
<small class="form-text text-muted">Must be at least 6 characters long</small>
|
238
|
+
```
|
239
|
+
|
239
240
|
This gem is also aware of help messages in locale translation files (i18n):
|
240
241
|
|
241
242
|
```yml
|
@@ -285,8 +286,9 @@ To add a class to the input group wrapper, use the `:input_group_class` option.
|
|
285
286
|
|
286
287
|
### Additional Form Group Attributes
|
287
288
|
|
288
|
-
|
289
|
-
|
289
|
+
Bootstrap mark-up dictates that most input field types have the label and input wrapped in a `div.form-group`.
|
290
|
+
|
291
|
+
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.
|
290
292
|
|
291
293
|
```erb
|
292
294
|
<%= f.text_field :name, wrapper: { class: 'has-warning', data: { foo: 'bar' } } %>
|
@@ -301,7 +303,7 @@ Which produces the following output:
|
|
301
303
|
</div>
|
302
304
|
```
|
303
305
|
|
304
|
-
|
306
|
+
If you only want to set the class on the form group div, you can use the `wrapper_class` option. It's just a short form of `wrapper: { class: 'additional-class' }`.
|
305
307
|
|
306
308
|
### Suppressing the Form Group Altogether
|
307
309
|
|
@@ -315,7 +317,7 @@ end
|
|
315
317
|
|
316
318
|
Note that Bootstrap relies on the form group div to correctly format most fields, so if you use the `wrapper: false` option, you should provide your own form group div around the input field. You can write your own HTML, or use the `form_group` helper.
|
317
319
|
|
318
|
-
|
320
|
+
## Selects
|
319
321
|
|
320
322
|
Our select helper accepts the same arguments as the [default Rails helper](http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select). Here's an example of how you pass both options and html_options hashes:
|
321
323
|
|
@@ -323,7 +325,7 @@ Our select helper accepts the same arguments as the [default Rails helper](http:
|
|
323
325
|
<%= f.select :product, [["Apple", 1], ["Grape", 2]], { label: "Choose your favorite fruit:", wrapper: { class: 'has-warning', data: { foo: 'bar' } } }, { class: "selectpicker" } %>
|
324
326
|
```
|
325
327
|
|
326
|
-
|
328
|
+
## Checkboxes and Radios
|
327
329
|
|
328
330
|
Checkboxes and radios should be placed inside of a `form_group` to render
|
329
331
|
properly. The following example ensures that the entire form group will display
|
@@ -366,15 +368,15 @@ Check boxes and radio buttons are wrapped in a `div.form-check`. You can add cla
|
|
366
368
|
```erb
|
367
369
|
<%= f.radio_button :skill_level, 0, label: "Novice", inline: true, wrapper_class: "w-auto" %>
|
368
370
|
```
|
369
|
-
|
371
|
+
### Switches
|
370
372
|
|
371
|
-
To render checkboxes as switches with Bootstrap 4.2+,
|
373
|
+
To render checkboxes as switches with Bootstrap 4.2+, use `custom: :switch`:
|
372
374
|
|
373
375
|
```erb
|
374
|
-
<%= f.check_box :remember_me, custom:
|
376
|
+
<%= f.check_box :remember_me, custom: :switch %>
|
375
377
|
```
|
376
378
|
|
377
|
-
|
379
|
+
### Collections
|
378
380
|
|
379
381
|
`bootstrap_form` also provides helpers that automatically create the
|
380
382
|
`form_group` and the `radio_button`s or `check_box`es for you:
|
@@ -384,13 +386,15 @@ To render checkboxes as switches with Bootstrap 4.2+, add the proper wrapper cla
|
|
384
386
|
<%= f.collection_check_boxes :skills, Skill.all, :id, :name %>
|
385
387
|
```
|
386
388
|
|
389
|
+
NOTE: These helpers do not currently support a block, unlike their equivalent Rails helpers. See issue #477.
|
390
|
+
|
387
391
|
Collection methods accept these options:
|
388
392
|
* `:label`: Customize the `form_group`'s label
|
389
393
|
* `:hide_label`: Pass true to hide the `form_group`'s label
|
390
394
|
* `:help`: Add a help span to the `form_group`
|
391
395
|
* Other options will be forwarded to the `radio_button`/`check_box` method
|
392
396
|
|
393
|
-
|
397
|
+
## Static Controls
|
394
398
|
|
395
399
|
You can create a static control like this:
|
396
400
|
|
@@ -412,12 +416,14 @@ Here's the output for a horizontal layout:
|
|
412
416
|
You can also create a static control that isn't based on a model attribute:
|
413
417
|
|
414
418
|
```erb
|
415
|
-
<%= f.static_control label: "Custom Static Control"
|
416
|
-
Content Here
|
417
|
-
<% end %>
|
419
|
+
<%= f.static_control label: "Custom Static Control" value: "Content Here" %>
|
418
420
|
```
|
421
|
+
Prior to version 4 of `bootstrap_form`, you could pass a block to the `static_control` method.
|
422
|
+
The value of the block would be used for the content of the static "control".
|
423
|
+
Bootstrap 4 actually creates and styles a disabled input field for static controls, so the value of the control has to be specified by the `value:` option.
|
424
|
+
Passing a block to `static_control` no longer has any effect.
|
419
425
|
|
420
|
-
|
426
|
+
## Date Helpers
|
421
427
|
|
422
428
|
The multiple selects that the date and time helpers (`date_select`,
|
423
429
|
`time_select`, `datetime_select`) generate are wrapped inside a
|
@@ -425,7 +431,7 @@ The multiple selects that the date and time helpers (`date_select`,
|
|
425
431
|
Bootstrap automatically styles our controls as `block`s. This wrapper fixes
|
426
432
|
this defining these selects as `inline-block` and a width of `auto`.
|
427
433
|
|
428
|
-
|
434
|
+
## Submit Buttons
|
429
435
|
|
430
436
|
The `btn btn-secondary` CSS classes are automatically added to your submit
|
431
437
|
buttons.
|
@@ -461,7 +467,7 @@ illustrative icons to them). For example, the following statements
|
|
461
467
|
end %>
|
462
468
|
```
|
463
469
|
|
464
|
-
are equivalent, and each of them both be rendered as
|
470
|
+
are equivalent, and each of them both be rendered as:
|
465
471
|
|
466
472
|
```html
|
467
473
|
<button name="button" type="submit" class="btn btn-primary">Save changes <span class="fa fa-save"></span></button>
|
@@ -490,7 +496,29 @@ will be rendered as
|
|
490
496
|
|
491
497
|
(some unimportant HTML attributes have been removed for simplicity)
|
492
498
|
|
493
|
-
|
499
|
+
## Rich Text Areas AKA Trix Editor
|
500
|
+
If you're using Rails 6, `bootstrap_form` supports the `rich_text_area` helper.
|
501
|
+
|
502
|
+
```
|
503
|
+
<%= f.rich_text_area(:life_story) %>
|
504
|
+
```
|
505
|
+
will be rendered as:
|
506
|
+
```
|
507
|
+
<div class="form-group">
|
508
|
+
<label for="user_life_story">Life story</label>
|
509
|
+
<input type="hidden" name="user[life_story]" id="user_life_story_trix_input_user"/>
|
510
|
+
<trix-editor id="user_life_story" data-blob-url-template="http://test.host/rails/active_storage/blobs/:signed_id/:filename" data-direct-upload-url="http://test.host/rails/active_storage/direct_uploads" input="user_life_story_trix_input_user" class="trix-content form-control"/>
|
511
|
+
</trix-editor>
|
512
|
+
</div>
|
513
|
+
```
|
514
|
+
|
515
|
+
## File Fields
|
516
|
+
The `file_field` helper generates mark-up for a Bootstrap 4 custom file field entry. It takes the [options for `text_field`](#form-helper-options), minus `append` and `prepend`.
|
517
|
+
|
518
|
+
## Hidden Fields
|
519
|
+
The `hidden_field` helper in `bootstrap_form` calls the Rails helper directly, and does no additional mark-up.
|
520
|
+
|
521
|
+
## Accessing Rails Form Helpers
|
494
522
|
|
495
523
|
If you want to use the original Rails form helpers for a particular field,
|
496
524
|
append `_without_bootstrap` to the helper:
|
@@ -502,7 +530,7 @@ append `_without_bootstrap` to the helper:
|
|
502
530
|
## Form Styles
|
503
531
|
|
504
532
|
By default, your forms will stack labels on top of controls and your controls
|
505
|
-
will grow to 100
|
533
|
+
will grow to 100 percent of the available width. This is consistent with Bootstrap's "mobile first" approach.
|
506
534
|
|
507
535
|
### Inline Forms
|
508
536
|
|
@@ -618,12 +646,12 @@ The `custom` option can be used to replace the browser default styles for check
|
|
618
646
|
```
|
619
647
|
|
620
648
|
## Validation and Errors
|
649
|
+
Rails normally wraps fields with validation errors in a `div.field_with_errors`, but this behaviour isn't consistent with Bootstrap 4 styling. By default, `bootstrap_form` generations in-line errors which appear below the field. But it can also generate errors on the label, or not display any errors, leaving it up to you.
|
621
650
|
|
622
651
|
### Inline Errors
|
623
652
|
|
624
653
|
By default, fields that have validation errors will be outlined in red and the
|
625
|
-
error will be displayed below the field.
|
626
|
-
div (field_with_errors), but this behavior is suppressed. Here's an example:
|
654
|
+
error will be displayed below the field. Here's an example:
|
627
655
|
|
628
656
|
```html
|
629
657
|
<div class="form-group">
|
@@ -729,11 +757,40 @@ Which outputs:
|
|
729
757
|
<div class="alert alert-danger">can't be blank.</div>
|
730
758
|
```
|
731
759
|
|
760
|
+
## Required Fields
|
761
|
+
|
762
|
+
A label that is associated with a required field is automatically annotated with
|
763
|
+
a `required` CSS class. `bootstrap_form` doesn't provide any styling for required fields. You're free to add any appropriate CSS to style
|
764
|
+
required fields as desired. One example would be to automatically add an
|
765
|
+
asterisk to the end of the label:
|
766
|
+
|
767
|
+
```css
|
768
|
+
label.required:after {
|
769
|
+
content:" *";
|
770
|
+
}
|
771
|
+
```
|
772
|
+
|
773
|
+
The label `required` class is determined based on the definition of a presence
|
774
|
+
validator with the associated model attribute. Presently this is one of:
|
775
|
+
ActiveRecord::Validations::PresenceValidator or
|
776
|
+
ActiveModel::Validations::PresenceValidator.
|
777
|
+
|
778
|
+
In cases where this behaviour is undesirable, use the `required` option to force the class to be present or absent:
|
779
|
+
|
780
|
+
```erb
|
781
|
+
<%= f.password_field :login, label: "New Username", required: true %>
|
782
|
+
<%= f.password_field :password, label: "New Password", required: false %>
|
783
|
+
```
|
784
|
+
|
732
785
|
## Internationalization
|
733
786
|
|
734
787
|
bootstrap_form follows standard rails conventions so it's i18n-ready. See more
|
735
788
|
here: http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models
|
736
789
|
|
790
|
+
## Future Compatibility
|
791
|
+
|
792
|
+
The Rails team has [suggested](https://github.com/rails/rails/issues/25197) that `form_for` and `form_tag` may be deprecated and then removed in future versions of Rails. `bootstrap_form` will continue to support `bootstrap_form_for` and `bootstrap_form_tag` as long as Rails supports `form_for` and `form_tag`.
|
793
|
+
|
737
794
|
## Other Tips and Edge Cases
|
738
795
|
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.
|
739
796
|
|
@@ -762,4 +819,4 @@ document first.
|
|
762
819
|
|
763
820
|
## License
|
764
821
|
|
765
|
-
MIT License. Copyright 2012-
|
822
|
+
MIT License. Copyright 2012-2019 Stephen Potenza (https://github.com/potenza)
|
data/Rakefile
CHANGED
@@ -31,9 +31,7 @@ task "release:rubygem_push" do
|
|
31
31
|
Rake.application.invoke_task("chandler:push")
|
32
32
|
end
|
33
33
|
|
34
|
-
desc 'Run RuboCop checks
|
35
|
-
RuboCop::RakeTask.new(:rubocop)
|
36
|
-
task.options = %w[--config .rubocop_todo.yml]
|
37
|
-
end
|
34
|
+
desc 'Run RuboCop checks'
|
35
|
+
RuboCop::RakeTask.new(:rubocop)
|
38
36
|
|
39
37
|
task default: %i[test rubocop]
|
data/bootstrap_form.gemspec
CHANGED
data/demo/.postcssrc.yml
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
//
|
2
|
+
// Provides a drop-in pointer for the default Trix stylesheet that will format the toolbar and
|
3
|
+
// the trix-editor content (whether displayed or under editing). Feel free to incorporate this
|
4
|
+
// inclusion directly in any other asset bundle and remove this file.
|
5
|
+
//
|
6
|
+
//= require trix/dist/trix
|
7
|
+
// When using `application.scss`, this has to be `import`, not `require`.
|
8
|
+
@import "trix/dist/trix";
|
9
|
+
|
10
|
+
// We need to override trix.css’s image gallery styles to accommodate the
|
11
|
+
// <action-text-attachment> element we wrap around attachments. Otherwise,
|
12
|
+
// images in galleries will be squished by the max-width: 33%; rule.
|
13
|
+
.trix-content {
|
14
|
+
.attachment-gallery {
|
15
|
+
> action-text-attachment,
|
16
|
+
> .attachment {
|
17
|
+
flex: 1 0 33%;
|
18
|
+
padding: 0 0.5em;
|
19
|
+
max-width: 33%;
|
20
|
+
}
|
21
|
+
|
22
|
+
&.attachment-gallery--2,
|
23
|
+
&.attachment-gallery--4 {
|
24
|
+
> action-text-attachment,
|
25
|
+
> .attachment {
|
26
|
+
flex-basis: 50%;
|
27
|
+
max-width: 50%;
|
28
|
+
}
|
29
|
+
}
|
30
|
+
}
|
31
|
+
|
32
|
+
action-text-attachment {
|
33
|
+
.attachment {
|
34
|
+
padding: 0 !important;
|
35
|
+
max-width: 100% !important;
|
36
|
+
}
|
37
|
+
}
|
38
|
+
}
|