Skip to content

Commit 54bfcab

Browse files
authored
Merge pull request #769 from lcreid/762-datetime-field
Add example for date_field
2 parents 734b26b + cc4f0ac commit 54bfcab

32 files changed

+55
-32
lines changed

.yarnrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
# yarn lockfile v1
33

44

5-
lastUpdateCheck 1761521327441
5+
lastUpdateCheck 1761695563757

README.md

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,17 @@ Then:
5959

6060
`bundle install`
6161

62-
`bootstrap_form` uses a very small number of its own CSS styles. Add the styles to your CSS bundle (usually your `application.scss` file). The way to do this depends on whether you're using Propshaft (the Rails 8 default), or Sprockets (pre-Rails 8). (Check your `Gemfile` to see whether you're using `sprockets-rails` or `propshaft`.)
62+
`bootstrap_form` uses a very small number of its own CSS styles. These styles are used in HTML generated by the `error_summary` and `alert_message` error helpers, and the `date_select`, `time_select`, and `datetime_select` helpers. If you're not using those helpers, you don't need to install the `bootstrap_form` CSS styles.
6363

64-
If you're using Propshaft, add the styles like this:
64+
If you do need the CSS styles, add them to your CSS bundle (usually your `application.scss` file). The way to do this depends on whether you're using Propshaft (the Rails 8 default), or Sprockets (pre-Rails 8). (Check your `Gemfile` to see whether you're using `sprockets-rails` or `propshaft`.)
65+
66+
If you're using Propshaft, add the styles to your CSS bundle like this:
6567

6668
```scss
6769
@use "rails_bootstrap_forms";
6870
```
6971

70-
If you're using Sprockets, add the styles like this:
72+
If you're using Sprockets, add the styles to your CSS bundle like this:
7173

7274
```scss
7375
@import "rails_bootstrap_forms.css";
@@ -939,7 +941,25 @@ The value of the block would be used for the content of the static "control".
939941
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.
940942
Passing a block to `static_control` no longer has any effect.
941943

942-
## Date Helpers
944+
## Date and Time Helpers
945+
946+
You can create a date picker, time picker, or date-time picker with `date_field`, `time_field`, or `datetime_field`, like this:
947+
948+
![Example 32](demo/doc/screenshots/bootstrap/readme/32_example.png "Example 32")
949+
```erb
950+
<%= f.date_field :joined_at, class: "w-auto" %>
951+
```
952+
953+
This generates:
954+
955+
```html
956+
<div class="mb-3">
957+
<label class="form-label" for="user_joined_at">Joined at</label>
958+
<input class="form-control w-auto" id="user_joined_at" name="user[joined_at]" type="date">
959+
</div>
960+
```
961+
962+
For backwards compatibility, there are also helpers for `date_select`, `time_select`, and `datetime_select`.
943963

944964
The multiple selects that the date and time helpers (`date_select`,
945965
`time_select`, `datetime_select`) generate are wrapped inside a
@@ -952,7 +972,7 @@ this by defining these selects as `inline-block` and a width of `auto`.
952972
The `btn btn-secondary` CSS classes are automatically added to your submit
953973
buttons.
954974

955-
![Example 32](demo/doc/screenshots/bootstrap/readme/32_example.png "Example 32")
975+
![Example 33](demo/doc/screenshots/bootstrap/readme/33_example.png "Example 33")
956976
```erb
957977
<%= f.submit %>
958978
```
@@ -966,7 +986,7 @@ This generates:
966986
You can also use the `primary` helper, which adds `btn btn-primary` to your
967987
submit button:
968988

969-
![Example 33](demo/doc/screenshots/bootstrap/readme/33_example.png "Example 33")
989+
![Example 34](demo/doc/screenshots/bootstrap/readme/34_example.png "Example 34")
970990
```erb
971991
<%= f.primary "Optional Label" %>
972992
```
@@ -979,7 +999,7 @@ This generates:
979999

9801000
You can specify your own classes like this:
9811001

982-
![Example 34](demo/doc/screenshots/bootstrap/readme/34_example.png "Example 34")
1002+
![Example 35](demo/doc/screenshots/bootstrap/readme/35_example.png "Example 35")
9831003
```erb
9841004
<%= f.submit "Log In", class: "btn btn-success" %>
9851005
```
@@ -995,7 +1015,7 @@ it will be rendered as an HTML button, instead of an input tag. This allows you
9951015
to specify HTML content and styling for your buttons (such as adding
9961016
illustrative icons to them). For example, the following statements
9971017

998-
![Example 35](demo/doc/screenshots/bootstrap/readme/35_example.png "Example 35")
1018+
![Example 36](demo/doc/screenshots/bootstrap/readme/36_example.png "Example 36")
9991019
```erb
10001020
<%= f.primary "Save changes <span class='bi bi-save'></span>".html_safe, render_as_button: true %>
10011021
@@ -1029,7 +1049,7 @@ Bootstrap classes), or for element targeting via CSS classes.
10291049
Be aware, however, that using the `class` option will discard any extra classes
10301050
you add. As an example, the following button declarations
10311051

1032-
![Example 36](demo/doc/screenshots/bootstrap/readme/36_example.png "Example 36")
1052+
![Example 37](demo/doc/screenshots/bootstrap/readme/37_example.png "Example 37")
10331053
```erb
10341054
<%= f.primary "My Nice Button", extra_class: 'my-button' %>
10351055
@@ -1047,7 +1067,7 @@ will be rendered as
10471067

10481068
## Rich Text Areas AKA Trix Editor
10491069

1050-
![Example 37](demo/doc/screenshots/bootstrap/readme/37_example.png "Example 37")
1070+
![Example 38](demo/doc/screenshots/bootstrap/readme/38_example.png "Example 38")
10511071
```erb
10521072
<%= f.rich_text_area(:life_story) %>
10531073
```
@@ -1115,7 +1135,7 @@ The `hidden_field` helper in `bootstrap_form` calls the Rails helper directly, a
11151135
If you want to use the original Rails form helpers for a particular field,
11161136
append `_without_bootstrap` to the helper:
11171137

1118-
![Example 38](demo/doc/screenshots/bootstrap/readme/38_example.png "Example 38")
1138+
![Example 39](demo/doc/screenshots/bootstrap/readme/39_example.png "Example 39")
11191139
```erb
11201140
<%= f.text_field_without_bootstrap :email %>
11211141
```
@@ -1137,7 +1157,7 @@ To use an inline-layout form, use the `layout: :inline` option. To hide labels,
11371157
use the `hide_label: true` option, which keeps your labels accessible to those
11381158
using screen readers.
11391159

1140-
![Example 39](demo/doc/screenshots/bootstrap/readme/39_example.png "Example 39")
1160+
![Example 40](demo/doc/screenshots/bootstrap/readme/40_example.png "Example 40")
11411161
```erb
11421162
<%= bootstrap_form_for(@user, layout: :inline) do |f| %>
11431163
<%= f.email_field :email, hide_label: true %>
@@ -1174,7 +1194,7 @@ This generates:
11741194

11751195
To skip label rendering at all, use `skip_label: true` option.
11761196

1177-
![Example 40](demo/doc/screenshots/bootstrap/readme/40_example.png "Example 40")
1197+
![Example 41](demo/doc/screenshots/bootstrap/readme/41_example.png "Example 41")
11781198
```erb
11791199
<%= f.password_field :password, skip_label: true %>
11801200
```
@@ -1196,7 +1216,7 @@ To use a horizontal-layout form with labels to the left of the control, use the
11961216
In the example below, the submit button has been wrapped in a `form_group` to
11971217
keep it properly aligned.
11981218

1199-
![Example 41](demo/doc/screenshots/bootstrap/readme/41_example.png "Example 41")
1219+
![Example 42](demo/doc/screenshots/bootstrap/readme/42_example.png "Example 42")
12001220
```erb
12011221
<%= bootstrap_form_for(@user, layout: :horizontal, label_col: "col-sm-2", control_col: "col-sm-10") do |f| %>
12021222
<%= f.email_field :email %>
@@ -1243,7 +1263,7 @@ This generates:
12431263

12441264
The `label_col` and `control_col` css classes can also be changed per control:
12451265

1246-
![Example 42](demo/doc/screenshots/bootstrap/readme/42_example.png "Example 42")
1266+
![Example 43](demo/doc/screenshots/bootstrap/readme/43_example.png "Example 43")
12471267
```erb
12481268
<%= bootstrap_form_for(@user, layout: :horizontal) do |f| %>
12491269
<%= f.email_field :email %>
@@ -1310,7 +1330,7 @@ end
13101330

13111331
Control col wrapper class can be modified with `add_control_col_class`. This option will preserve column definition:
13121332

1313-
![Example 43](demo/doc/screenshots/bootstrap/readme/43_example.png "Example 43")
1333+
![Example 44](demo/doc/screenshots/bootstrap/readme/44_example.png "Example 44")
13141334
```erb
13151335
<%= bootstrap_form_for(@user, layout: :horizontal) do |f| %>
13161336
<%= f.email_field :email %>
@@ -1349,7 +1369,7 @@ This generates:
13491369

13501370
The form-level `layout` can be overridden per field, unless the form-level layout was `inline`:
13511371

1352-
![Example 44](demo/doc/screenshots/bootstrap/readme/44_example.png "Example 44")
1372+
![Example 45](demo/doc/screenshots/bootstrap/readme/45_example.png "Example 45")
13531373
```erb
13541374
<%= bootstrap_form_for(@user, layout: :horizontal) do |f| %>
13551375
<%= f.email_field :email %>
@@ -1404,7 +1424,7 @@ A form-level `layout: :inline` can't be overridden because of the way Bootstrap
14041424
The `floating` option can be used to enable Bootstrap 5's floating labels. This option is supported on text fields
14051425
and dropdowns. Here's an example:
14061426

1407-
![Example 45](demo/doc/screenshots/bootstrap/readme/45_example.png "Example 45")
1427+
![Example 46](demo/doc/screenshots/bootstrap/readme/46_example.png "Example 46")
14081428
```erb
14091429
<%= bootstrap_form_for(@user) do |f| %>
14101430
<%= f.email_field :email, floating: true %>
@@ -1452,7 +1472,7 @@ Rails normally wraps fields with validation errors in a `div.field_with_errors`,
14521472
By default, fields that have validation errors will be outlined in red and the
14531473
error will be displayed below the field. Here's an example:
14541474

1455-
![Example 46](demo/doc/screenshots/bootstrap/readme/46_example.png "Example 46")
1475+
![Example 47](demo/doc/screenshots/bootstrap/readme/47_example.png "Example 47")
14561476
```erb
14571477
<%= bootstrap_form_for(@user_with_error) do |f| %>
14581478
<%= f.email_field :email %>
@@ -1519,7 +1539,7 @@ You can turn off inline errors for the entire form like this:
15191539
You can also display validation errors in the field's label; just turn
15201540
on the `:label_errors` option. Here's an example:
15211541

1522-
![Example 47](demo/doc/screenshots/bootstrap/readme/47_example.png "Example 47")
1542+
![Example 48](demo/doc/screenshots/bootstrap/readme/48_example.png "Example 48")
15231543
```erb
15241544
<%= bootstrap_form_for(@user_with_error, label_errors: true) do |f| %>
15251545
<%= f.email_field :email %>
@@ -1552,7 +1572,7 @@ To display an error message with an error summary, you can use the
15521572
`alert_message` helper. This won't output anything unless a model validation
15531573
has failed.
15541574

1555-
![Example 48](demo/doc/screenshots/bootstrap/readme/48_example.png "Example 48")
1575+
![Example 49](demo/doc/screenshots/bootstrap/readme/49_example.png "Example 49")
15561576
```erb
15571577
<%= bootstrap_form_for @user_with_error do |f| %>
15581578
<%= f.alert_message "Please fix the errors below." %>
@@ -1576,7 +1596,7 @@ Which outputs:
15761596

15771597
You can turn off the error summary like this:
15781598

1579-
![Example 49](demo/doc/screenshots/bootstrap/readme/49_example.png "Example 49")
1599+
![Example 50](demo/doc/screenshots/bootstrap/readme/50_example.png "Example 50")
15801600
```erb
15811601
<%= bootstrap_form_for @user_with_error do |f| %>
15821602
<%= f.alert_message "Please fix the errors below.", error_summary: false %>
@@ -1593,7 +1613,7 @@ This generates:
15931613

15941614
To output a simple unordered list of errors, use the `error_summary` helper.
15951615

1596-
![Example 50](demo/doc/screenshots/bootstrap/readme/50_example.png "Example 50")
1616+
![Example 51](demo/doc/screenshots/bootstrap/readme/51_example.png "Example 51")
15971617
```erb
15981618
<%= bootstrap_form_for @user_with_error do |f| %>
15991619
<%= f.error_summary %>
@@ -1616,7 +1636,7 @@ Which outputs:
16161636

16171637
If you want to display a custom inline error for a specific attribute not represented by a form field, use the `errors_on` helper.
16181638

1619-
![Example 51](demo/doc/screenshots/bootstrap/readme/51_example.png "Example 51")
1639+
![Example 52](demo/doc/screenshots/bootstrap/readme/52_example.png "Example 52")
16201640
```erb
16211641
<%= bootstrap_form_for @user_with_error do |f| %>
16221642
<input class="is-invalid" autocomplete="off" disabled type="hidden">
@@ -1637,7 +1657,7 @@ Note that the `invalid-feedback` `div` is hidden unless there is a preceding ele
16371657

16381658
You can hide the attribute name like this:
16391659

1640-
![Example 52](demo/doc/screenshots/bootstrap/readme/52_example.png "Example 52")
1660+
![Example 53](demo/doc/screenshots/bootstrap/readme/53_example.png "Example 53")
16411661
```erb
16421662
<%= bootstrap_form_for @user_with_error do |f| %>
16431663
<input class="is-invalid" autocomplete="off" disabled type="hidden">
@@ -1656,7 +1676,7 @@ Which outputs:
16561676

16571677
You can also use a custom class for the wrapping div, like this:
16581678

1659-
![Example 53](demo/doc/screenshots/bootstrap/readme/53_example.png "Example 53")
1679+
![Example 54](demo/doc/screenshots/bootstrap/readme/54_example.png "Example 54")
16601680
```erb
16611681
<%= bootstrap_form_for @user_with_error do |f| %>
16621682
<input class="is-invalid" autocomplete="off" disabled type="hidden">
@@ -1695,7 +1715,7 @@ ActiveModel::Validations::PresenceValidator.
16951715

16961716
In cases where this behaviour is undesirable, use the `required` option to force the class to be present or absent:
16971717

1698-
![Example 54](demo/doc/screenshots/bootstrap/readme/54_example.png "Example 54")
1718+
![Example 55](demo/doc/screenshots/bootstrap/readme/55_example.png "Example 55")
16991719
```erb
17001720
<%= f.password_field :login, label: "New Username", required: true %>
17011721
<%= f.password_field :password, label: "New Password", required: false %>
@@ -1718,7 +1738,7 @@ This generates:
17181738

17191739
Adding a form control for a `belongs_to` field will automatically pick up the associated presence validator.
17201740

1721-
![Example 55](demo/doc/screenshots/bootstrap/readme/55_example.png "Example 55")
1741+
![Example 56](demo/doc/screenshots/bootstrap/readme/56_example.png "Example 56")
17221742
```erb
17231743
<%= bootstrap_form_for(@address, url: '/address') do |f| %>
17241744
<%= f.collection_select :user_id, @users, :id, :email, include_blank: "Select a value" %>
@@ -1765,7 +1785,7 @@ Generated HTML:
17651785

17661786
Fields can be disabled using the standard Rails form helper option.
17671787

1768-
![Example 56](demo/doc/screenshots/bootstrap/readme/56_example.png "Example 56")
1788+
![Example 57](demo/doc/screenshots/bootstrap/readme/57_example.png "Example 57")
17691789
```erb
17701790
<%= bootstrap_form_for @user do |f| %>
17711791
<div class="row g-3">

compose.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,8 @@ services:
2929
tty: true
3030
environment:
3131
- LANG=en_CA.UTF-8
32+
- LANGUAGE=en_CA.UTF-8
33+
- LANG_WHERE=CA
34+
- LANG_WHICH=en
3235
ports:
3336
- "7900"

demo/app/views/bootstrap/form.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<%= form.collection_radio_buttons :radio_buttons, @collection, :id, :street %>
1212
<%= form.file_field :file %>
1313
<%= form.datetime_select :created_at, include_blank: true %>
14+
<%= form.datetime_field :updated_at, class: "w-auto" %>
1415

1516
<%= form.submit %>
1617
<% end %>
4.55 KB
Loading
1.84 KB
Loading
-118 Bytes
Loading
937 Bytes
Loading
-2.97 KB
Loading
406 Bytes
Loading

0 commit comments

Comments
 (0)