simple_form 2.1.3 → 3.0.0.rc
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +13 -50
- data/README.md +140 -116
- data/lib/generators/simple_form/install_generator.rb +4 -4
- data/lib/generators/simple_form/templates/README +2 -2
- data/lib/generators/simple_form/templates/config/initializers/simple_form.rb +13 -13
- data/lib/generators/simple_form/templates/config/initializers/simple_form_bootstrap.rb +16 -16
- data/lib/generators/simple_form/templates/config/initializers/simple_form_foundation.rb +3 -3
- data/lib/simple_form/action_view_extensions/builder.rb +1 -320
- data/lib/simple_form/action_view_extensions/form_helper.rb +2 -9
- data/lib/simple_form/components/errors.rb +1 -7
- data/lib/simple_form/components/hints.rb +2 -7
- data/lib/simple_form/components/html5.rb +5 -2
- data/lib/simple_form/components/labels.rb +4 -4
- data/lib/simple_form/components/maxlength.rb +1 -8
- data/lib/simple_form/components/pattern.rb +2 -2
- data/lib/simple_form/components.rb +1 -1
- data/lib/simple_form/error_notification.rb +2 -2
- data/lib/simple_form/form_builder.rb +148 -50
- data/lib/simple_form/helpers.rb +1 -1
- data/lib/simple_form/inputs/base.rb +3 -10
- data/lib/simple_form/inputs/block_input.rb +1 -1
- data/lib/simple_form/inputs/boolean_input.rb +6 -6
- data/lib/simple_form/inputs/collection_input.rb +7 -7
- data/lib/simple_form/inputs/numeric_input.rb +0 -6
- data/lib/simple_form/inputs/password_input.rb +0 -1
- data/lib/simple_form/inputs/string_input.rb +0 -1
- data/lib/simple_form/railtie.rb +7 -0
- data/lib/simple_form/tags.rb +61 -0
- data/lib/simple_form/version.rb +1 -1
- data/lib/simple_form/wrappers/builder.rb +5 -29
- data/lib/simple_form/wrappers/many.rb +1 -1
- data/lib/simple_form/wrappers/root.rb +1 -1
- data/lib/simple_form/wrappers.rb +1 -1
- data/lib/simple_form.rb +39 -47
- data/test/action_view_extensions/builder_test.rb +75 -95
- data/test/action_view_extensions/form_helper_test.rb +25 -16
- data/test/components/label_test.rb +46 -46
- data/test/form_builder/association_test.rb +39 -29
- data/test/form_builder/button_test.rb +4 -4
- data/test/form_builder/error_notification_test.rb +8 -8
- data/test/form_builder/error_test.rb +18 -65
- data/test/form_builder/general_test.rb +60 -74
- data/test/form_builder/hint_test.rb +23 -29
- data/test/form_builder/input_field_test.rb +12 -12
- data/test/form_builder/label_test.rb +6 -16
- data/test/form_builder/wrapper_test.rb +21 -21
- data/test/inputs/boolean_input_test.rb +23 -35
- data/test/inputs/collection_check_boxes_input_test.rb +61 -55
- data/test/inputs/collection_radio_buttons_input_test.rb +76 -79
- data/test/inputs/collection_select_input_test.rb +70 -51
- data/test/inputs/datetime_input_test.rb +17 -11
- data/test/inputs/disabled_test.rb +10 -10
- data/test/inputs/discovery_test.rb +4 -4
- data/test/inputs/file_input_test.rb +1 -1
- data/test/inputs/general_test.rb +12 -12
- data/test/inputs/grouped_collection_select_input_test.rb +20 -20
- data/test/inputs/hidden_input_test.rb +3 -2
- data/test/inputs/numeric_input_test.rb +3 -3
- data/test/inputs/priority_input_test.rb +9 -3
- data/test/inputs/readonly_test.rb +12 -12
- data/test/inputs/required_test.rb +5 -5
- data/test/inputs/string_input_test.rb +15 -25
- data/test/inputs/text_input_test.rb +1 -1
- data/test/support/misc_helpers.rb +46 -24
- data/test/support/mock_controller.rb +6 -6
- data/test/support/models.rb +62 -64
- data/test/test_helper.rb +20 -24
- metadata +37 -23
- data/lib/simple_form/core_ext/hash.rb +0 -16
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: eebdc48d53472ed80e668fd6c2fa30ebf30695a1
|
|
4
|
+
data.tar.gz: 368a258b70c60fcb73ed568a21f357b39c1fa93f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: fd1c37c0fffa1d3f3b5a06d19790f37a6aec133ab9d98d792c0c60ba40ea64628085ef8f26ab8f44a50bb17af3b0ee3e8dfdcd6d2997dfea021bc41240c300ed
|
|
7
|
+
data.tar.gz: ee7024a3684a698d0267fd2469723bfca7775270f064b0e11680cc85891c8d6d235189bd608d636fc551c7d3726b3d8167577ebc49869bc8c6969254c7ee5d00
|
data/CHANGELOG.md
CHANGED
|
@@ -1,57 +1,20 @@
|
|
|
1
|
-
##
|
|
1
|
+
## master
|
|
2
2
|
|
|
3
|
-
###
|
|
4
|
-
*
|
|
5
|
-
* Collection input generates `required` attribute if it has `prompt` option. [@nashby](https://github.com/nashby)
|
|
6
|
-
* Use the given name in `input_html` for the hidden field in `collection_check_boxes` [@angelic](https://github.com/angelic)
|
|
7
|
-
* Add "checkbox" class to the label of boolean input when there is no `:label`
|
|
8
|
-
in `generate_additional_classes_for` config option [@nashby](https://github.com/nashby)
|
|
9
|
-
* Support models with digits in their names [@webgago](https://github.com/webgago)
|
|
10
|
-
|
|
11
|
-
## 2.1.2
|
|
12
|
-
|
|
13
|
-
### bug fix
|
|
14
|
-
* Fix XSS vulnerability on error components.
|
|
15
|
-
|
|
16
|
-
## 2.1.1
|
|
3
|
+
### enhancements
|
|
4
|
+
* Make `field_error_proc` configurable [@dfens](https://github.com/dfens)
|
|
17
5
|
|
|
18
6
|
### bug fix
|
|
19
|
-
*
|
|
7
|
+
* Remove deprecation warnings related to `Relation#all` from Rails 4.
|
|
8
|
+
* Form builder can be used outside the context of a controller [@jasonwebster](https://github.com/jasonwebster)
|
|
9
|
+
* Skip pattern attribute when using `validates_format_of` with `:without` option [@glebm](https://github.com/glebm)
|
|
20
10
|
|
|
21
|
-
##
|
|
11
|
+
## 3.0.0.beta1
|
|
22
12
|
|
|
23
13
|
### enhancements
|
|
24
|
-
*
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
*
|
|
29
|
-
Closes [#478](https://github.com/plataformatec/simple_form/issues/478).
|
|
30
|
-
* generate required attribute for `date_time` input.
|
|
31
|
-
([@nashby](https://github.com/nashby))
|
|
32
|
-
Closes [#730](https://github.com/plataformatec/simple_form/issues/730).
|
|
33
|
-
* `grouped_collection_select` now accepts proc/lambda as label and value method.
|
|
34
|
-
([@svendahlstrand](https://github.com/svendahlstrand))
|
|
35
|
-
Closes [#623](https://github.com/plataformatec/simple_form/issues/623).
|
|
36
|
-
* Add Zurb Foundation 3 integration.
|
|
37
|
-
([@balexand](https://github.com/balexand))
|
|
38
|
-
* Generates additional wrapper class based on object + attribute name.
|
|
39
|
-
([@lucasmazza](https://github.com/lucasmazza))
|
|
40
|
-
Closes [#576](https://github.com/plataformatec/simple_form/issues/576).
|
|
41
|
-
* Allow `input_field` to work with `:defaults` options.
|
|
42
|
-
([@smidwap](https://github.com/smidwap))
|
|
43
|
-
|
|
44
|
-
### bug fix
|
|
45
|
-
* Do not lookup for hints if it was explicitly given false.
|
|
46
|
-
After #405 we added hint classes for the wrappers, but this has forced the
|
|
47
|
-
loading of the hint text doing I18n lookups, even though it was explicitly
|
|
48
|
-
given false. This checks for the option in `#has_hint?`, avoiding the lookup
|
|
49
|
-
in such cases. This issues has been caught with #627, thanks to
|
|
50
|
-
([@shwoodard](https://github.com/shwoodard)).
|
|
51
|
-
* Fix default I18n lookup for association input.
|
|
52
|
-
([@nashby](https://github.com/nashby))
|
|
53
|
-
Closes [#679](https://github.com/plataformatec/simple_form/issues/679).
|
|
54
|
-
* Fix escaping issue in `label_input` component
|
|
55
|
-
([@allomov](https://github.com/allomov))
|
|
14
|
+
* Support to Rails 4.
|
|
15
|
+
* Removed deprecated methods.
|
|
16
|
+
* SimpleForm no longer sets the `size` attribute automatically and the `default_input_size` setting
|
|
17
|
+
is now deprecated.
|
|
18
|
+
* Support to aria-required attribute to required fields [@ckundo](https://github.com/ckundo)
|
|
56
19
|
|
|
57
|
-
Please check [v2.
|
|
20
|
+
Please check [v2.1](https://github.com/plataformatec/simple_form/blob/v2.1/CHANGELOG.md) for previous changes.
|
data/README.md
CHANGED
|
@@ -1,12 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+

|
|
2
|
+
|
|
3
|
+
By [Plataformatec](http://plataformatec.com.br/).
|
|
4
|
+
|
|
5
|
+
[](http://badge.fury.io/rb/simple_form)
|
|
6
|
+
[](http://travis-ci.org/plataformatec/simple_form)
|
|
7
|
+
[](https://codeclimate.com/github/plataformatec/simple_form)
|
|
8
|
+
|
|
9
|
+
Rails forms made easy.
|
|
3
10
|
|
|
4
11
|
**SimpleForm** aims to be as flexible as possible while helping you with powerful components to create
|
|
5
12
|
your forms. The basic goal of SimpleForm is to not touch your way of defining the layout, letting
|
|
6
13
|
you find the better design for your eyes. Most of the DSL was inherited from Formtastic,
|
|
7
14
|
which we are thankful for and should make you feel right at home.
|
|
8
15
|
|
|
9
|
-
INFO: This README
|
|
16
|
+
INFO: This README is [also available in a friendly navigable format](http://simple-form.plataformatec.com.br/)
|
|
17
|
+
and refers to **SimpleForm** 3.0. For older releases, check the related branch for your version.
|
|
10
18
|
|
|
11
19
|
## Installation
|
|
12
20
|
|
|
@@ -35,23 +43,23 @@ Also, if you want to use the country select, you will need the
|
|
|
35
43
|
gem 'country_select'
|
|
36
44
|
```
|
|
37
45
|
|
|
38
|
-
### Bootstrap
|
|
46
|
+
### Twitter Bootstrap
|
|
39
47
|
|
|
40
|
-
**
|
|
48
|
+
**SimpleForm** can be easily integrated to the [Twitter Bootstrap](http://twitter.github.com/bootstrap).
|
|
41
49
|
To do that you have to use the `bootstrap` option in the install generator, like this:
|
|
42
50
|
|
|
43
51
|
```console
|
|
44
52
|
rails generate simple_form:install --bootstrap
|
|
45
53
|
```
|
|
46
54
|
|
|
47
|
-
You have to be sure that you added a copy of the [Bootstrap](http://
|
|
55
|
+
You have to be sure that you added a copy of the [Twitter Bootstrap](http://twitter.github.com/bootstrap)
|
|
48
56
|
assets on your application.
|
|
49
57
|
|
|
50
58
|
For more information see the generator output, our
|
|
51
59
|
[example application code](https://github.com/rafaelfranca/simple_form-bootstrap) and
|
|
52
60
|
[the live example app](http://simple-form-bootstrap.plataformatec.com.br/).
|
|
53
61
|
|
|
54
|
-
**NOTE**: **SimpleForm** integration requires Twitter Bootstrap version 2.0
|
|
62
|
+
**NOTE**: **SimpleForm** integration requires Twitter Bootstrap version 2.0 or higher.
|
|
55
63
|
|
|
56
64
|
### Zurb Foundation 3
|
|
57
65
|
|
|
@@ -63,7 +71,7 @@ rails generate simple_form:install --foundation
|
|
|
63
71
|
|
|
64
72
|
Please note that the Foundation wrapper does not support the `:hint` option by default. In order to enable hints, please uncomment the appropriate line in `config/initializers/simple_form_foundation.rb`. You will need to provide your own CSS styles for hints.
|
|
65
73
|
|
|
66
|
-
Please see the [instructions on how to install Foundation in a Rails app](http://foundation.zurb.com/docs/rails.php).
|
|
74
|
+
Please see the [instructions on how to install Foundation in a Rails app](http://foundation.zurb.com/old-docs/f3/rails.php).
|
|
67
75
|
|
|
68
76
|
## Usage
|
|
69
77
|
|
|
@@ -91,10 +99,10 @@ even a placeholder. For boolean inputs, you can add an inline label as well:
|
|
|
91
99
|
|
|
92
100
|
```erb
|
|
93
101
|
<%= simple_form_for @user do |f| %>
|
|
94
|
-
<%= f.input :username, :
|
|
95
|
-
<%= f.input :password, :
|
|
96
|
-
<%= f.input :email, :
|
|
97
|
-
<%= f.input :remember_me, :
|
|
102
|
+
<%= f.input :username, label: 'Your username please' %>
|
|
103
|
+
<%= f.input :password, hint: 'No special characters.' %>
|
|
104
|
+
<%= f.input :email, placeholder: 'user@domain.com' %>
|
|
105
|
+
<%= f.input :remember_me, inline_label: 'Yes, remember me' %>
|
|
98
106
|
<%= f.button :submit %>
|
|
99
107
|
<% end %>
|
|
100
108
|
```
|
|
@@ -104,9 +112,9 @@ of any of them:
|
|
|
104
112
|
|
|
105
113
|
```erb
|
|
106
114
|
<%= simple_form_for @user do |f| %>
|
|
107
|
-
<%= f.input :username, :
|
|
108
|
-
<%= f.input :password, :
|
|
109
|
-
<%= f.input :password_confirmation, :
|
|
115
|
+
<%= f.input :username, label_html: { class: 'my_class' } %>
|
|
116
|
+
<%= f.input :password, hint: false, error_html: { id: 'password_error'} %>
|
|
117
|
+
<%= f.input :password_confirmation, label: false %>
|
|
110
118
|
<%= f.button :submit %>
|
|
111
119
|
<% end %>
|
|
112
120
|
```
|
|
@@ -116,9 +124,9 @@ option, for instance:
|
|
|
116
124
|
|
|
117
125
|
```erb
|
|
118
126
|
<%= simple_form_for @user do |f| %>
|
|
119
|
-
<%= f.input :username, :
|
|
120
|
-
<%= f.input :password, :
|
|
121
|
-
<%= f.input :remember_me, :
|
|
127
|
+
<%= f.input :username, input_html: { class: 'special' } %>
|
|
128
|
+
<%= f.input :password, input_html: { maxlength: 20 } %>
|
|
129
|
+
<%= f.input :remember_me, input_html: { value: '1' } %>
|
|
122
130
|
<%= f.button :submit %>
|
|
123
131
|
<% end %>
|
|
124
132
|
```
|
|
@@ -128,10 +136,10 @@ you can use the `:defaults` option in `simple_form_for`. Specific options in `in
|
|
|
128
136
|
overwrite the defaults:
|
|
129
137
|
|
|
130
138
|
```erb
|
|
131
|
-
<%= simple_form_for @user, :
|
|
132
|
-
<%= f.input :username, :
|
|
133
|
-
<%= f.input :password, :
|
|
134
|
-
<%= f.input :remember_me, :
|
|
139
|
+
<%= simple_form_for @user, defaults: { input_html: { class: 'default_class' } } do |f| %>
|
|
140
|
+
<%= f.input :username, input_html: { class: 'special' } %>
|
|
141
|
+
<%= f.input :password, input_html: { maxlength: 20 } %>
|
|
142
|
+
<%= f.input :remember_me, input_html: { value: '1' } %>
|
|
135
143
|
<%= f.button :submit %>
|
|
136
144
|
<% end %>
|
|
137
145
|
```
|
|
@@ -141,9 +149,9 @@ any html attribute to that wrapper as well using the `:wrapper_html` option, lik
|
|
|
141
149
|
|
|
142
150
|
```erb
|
|
143
151
|
<%= simple_form_for @user do |f| %>
|
|
144
|
-
<%= f.input :username, :
|
|
145
|
-
<%= f.input :password, :
|
|
146
|
-
<%= f.input :remember_me, :
|
|
152
|
+
<%= f.input :username, wrapper_html: { class: 'username' } %>
|
|
153
|
+
<%= f.input :password, wrapper_html: { id: 'password' } %>
|
|
154
|
+
<%= f.input :remember_me, wrapper_html: { class: 'options' } %>
|
|
147
155
|
<%= f.button :submit %>
|
|
148
156
|
<% end %>
|
|
149
157
|
```
|
|
@@ -156,35 +164,28 @@ And of course, the `required` property of any input can be overwritten as needed
|
|
|
156
164
|
|
|
157
165
|
```erb
|
|
158
166
|
<%= simple_form_for @user do |f| %>
|
|
159
|
-
<%= f.input :name, :
|
|
167
|
+
<%= f.input :name, required: false %>
|
|
160
168
|
<%= f.input :username %>
|
|
161
169
|
<%= f.input :password %>
|
|
162
170
|
<%= f.button :submit %>
|
|
163
171
|
<% end %>
|
|
164
172
|
```
|
|
165
173
|
|
|
166
|
-
By default, SimpleForm will look at the column type in the database and use an
|
|
167
|
-
appropriate input for the column. For example, a column created with type
|
|
168
|
-
`:text` in the database will use a `textarea` input by default. See the section
|
|
169
|
-
[Available input types and defaults for each column
|
|
170
|
-
type](https://github.com/plataformatec/simple_form#available-input-types-and-defaults-for-each-column-type)
|
|
171
|
-
for a complete list of defaults.
|
|
172
|
-
|
|
173
174
|
**SimpleForm** also lets you overwrite the default input type it creates:
|
|
174
175
|
|
|
175
176
|
```erb
|
|
176
177
|
<%= simple_form_for @user do |f| %>
|
|
177
178
|
<%= f.input :username %>
|
|
178
179
|
<%= f.input :password %>
|
|
179
|
-
<%= f.input :description, :
|
|
180
|
-
<%= f.input :accepts, :
|
|
180
|
+
<%= f.input :description, as: :text %>
|
|
181
|
+
<%= f.input :accepts, as: :radio_buttons %>
|
|
181
182
|
<%= f.button :submit %>
|
|
182
183
|
<% end %>
|
|
183
184
|
```
|
|
184
185
|
|
|
185
186
|
So instead of a checkbox for the *accepts* attribute, you'll have a pair of radio buttons with yes/no
|
|
186
|
-
labels and a
|
|
187
|
-
attributes using
|
|
187
|
+
labels and a text area instead of a text field for the description. You can also render boolean
|
|
188
|
+
attributes using `as: :select` to show a dropdown.
|
|
188
189
|
|
|
189
190
|
It is also possible to give the `:disabled` option to **SimpleForm**, and it'll automatically mark
|
|
190
191
|
the wrapper as disabled with a css class, so you can style labels, hints and other components inside
|
|
@@ -192,7 +193,7 @@ the wrapper as well:
|
|
|
192
193
|
|
|
193
194
|
```erb
|
|
194
195
|
<%= simple_form_for @user do |f| %>
|
|
195
|
-
<%= f.input :username, :
|
|
196
|
+
<%= f.input :username, disabled: true, hint: 'You cannot change your username.' %>
|
|
196
197
|
<%= f.button :submit %>
|
|
197
198
|
<% end %>
|
|
198
199
|
```
|
|
@@ -201,9 +202,9 @@ the wrapper as well:
|
|
|
201
202
|
|
|
202
203
|
```erb
|
|
203
204
|
<%= simple_form_for @user do |f| %>
|
|
204
|
-
<%= f.input :date_of_birth, :
|
|
205
|
-
:
|
|
206
|
-
:
|
|
205
|
+
<%= f.input :date_of_birth, as: :date, start_year: Date.today.year - 90,
|
|
206
|
+
end_year: Date.today.year - 12, discard_day: true,
|
|
207
|
+
order: [:month, :year] %>
|
|
207
208
|
<%= f.button :submit %>
|
|
208
209
|
<% end %>
|
|
209
210
|
```
|
|
@@ -216,7 +217,7 @@ the wrapper as well:
|
|
|
216
217
|
<%= f.label :username %>
|
|
217
218
|
<%= f.input_field :username %>
|
|
218
219
|
<%= f.hint 'No special characters, please!' %>
|
|
219
|
-
<%= f.error :username, :
|
|
220
|
+
<%= f.error :username, id: 'user_name_error' %>
|
|
220
221
|
<%= f.full_error :token %>
|
|
221
222
|
<%= f.submit 'Save' %>
|
|
222
223
|
<% end %>
|
|
@@ -224,6 +225,28 @@ the wrapper as well:
|
|
|
224
225
|
|
|
225
226
|
Any extra option passed to these methods will be rendered as html option.
|
|
226
227
|
|
|
228
|
+
### Stripping away all wrapper divs
|
|
229
|
+
|
|
230
|
+
**SimpleForm** also allows you to strip away all the div wrappers around the `<input>` field that is generated with the usual `f.input`.
|
|
231
|
+
The easiest way to achieve this is to use `f.input_field`.
|
|
232
|
+
|
|
233
|
+
Example:
|
|
234
|
+
|
|
235
|
+
```erb
|
|
236
|
+
simple_form_for @user do |f|
|
|
237
|
+
f.input_field :name
|
|
238
|
+
end
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
Produces:
|
|
242
|
+
|
|
243
|
+
```erb
|
|
244
|
+
<input class="string required" id="user_name" maxlength="100"
|
|
245
|
+
name="user[name]" size="100" type="text" value="Carlos" />
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
To view the actual RDocs for this, check them out here - http://rubydoc.info/github/plataformatec/simple_form/master/SimpleForm/FormBuilder:input_field
|
|
249
|
+
|
|
227
250
|
### Collections
|
|
228
251
|
|
|
229
252
|
And what if you want to create a select containing the age from 18 to 60 in your form? You can do it
|
|
@@ -232,13 +255,13 @@ overriding the `:collection` option:
|
|
|
232
255
|
```erb
|
|
233
256
|
<%= simple_form_for @user do |f| %>
|
|
234
257
|
<%= f.input :user %>
|
|
235
|
-
<%= f.input :age, :
|
|
258
|
+
<%= f.input :age, collection: 18..60 %>
|
|
236
259
|
<%= f.button :submit %>
|
|
237
260
|
<% end %>
|
|
238
261
|
```
|
|
239
262
|
|
|
240
263
|
Collections can be arrays or ranges, and when a `:collection` is given the `:select` input will be
|
|
241
|
-
rendered by default, so we don't need to pass the
|
|
264
|
+
rendered by default, so we don't need to pass the `as: :select` option. Other types of collection
|
|
242
265
|
are `:radio_buttons` and `:check_boxes`. Those are added by **SimpleForm** to Rails set of form
|
|
243
266
|
helpers (read Extra Helpers session below for more information).
|
|
244
267
|
|
|
@@ -255,13 +278,13 @@ translation. All other options given are sent straight to the underlying helper.
|
|
|
255
278
|
can give prompt as:
|
|
256
279
|
|
|
257
280
|
```ruby
|
|
258
|
-
f.input :age, :
|
|
281
|
+
f.input :age, collection: 18..60, prompt: "Select your age"
|
|
259
282
|
```
|
|
260
283
|
|
|
261
284
|
It is also possible to create grouped collection selects, that will use the html *optgroup* tags, like this:
|
|
262
285
|
|
|
263
286
|
```ruby
|
|
264
|
-
f.input :country_id, :
|
|
287
|
+
f.input :country_id, collection: @continents, as: :grouped_select, group_method: :countries
|
|
265
288
|
```
|
|
266
289
|
|
|
267
290
|
Grouped collection inputs accept the same `:label_method` and `:value_method` options, which will be
|
|
@@ -280,8 +303,8 @@ used to retrieve label/value attributes for the `option` tags. Besides that, you
|
|
|
280
303
|
`:priority` as option to select which time zones and/or countries should be given higher priority:
|
|
281
304
|
|
|
282
305
|
```ruby
|
|
283
|
-
f.input :residence_country, :
|
|
284
|
-
f.input :time_zone, :
|
|
306
|
+
f.input :residence_country, priority: [ "Brazil" ]
|
|
307
|
+
f.input :time_zone, priority: /US/
|
|
285
308
|
```
|
|
286
309
|
|
|
287
310
|
Those values can also be configured with a default value to be used site use through the
|
|
@@ -291,12 +314,12 @@ Note: While using `country_select` if you want to restrict to only a subset of c
|
|
|
291
314
|
drop down then you may use the `:collection` option:
|
|
292
315
|
|
|
293
316
|
```ruby
|
|
294
|
-
f.input :shipping_country, :
|
|
317
|
+
f.input :shipping_country, priority: [ "Brazil" ], collection: [ "Australia", "Brazil", "New Zealand"]
|
|
295
318
|
```
|
|
296
319
|
|
|
297
320
|
### Associations
|
|
298
321
|
|
|
299
|
-
To deal with associations, **SimpleForm** can generate select inputs, a series of radios buttons or
|
|
322
|
+
To deal with associations, **SimpleForm** can generate select inputs, a series of radios buttons or check boxes.
|
|
300
323
|
Lets see how it works: imagine you have a user model that belongs to a company and has_and_belongs_to_many
|
|
301
324
|
roles. The structure would be something like:
|
|
302
325
|
|
|
@@ -328,11 +351,11 @@ Now we have the user form:
|
|
|
328
351
|
|
|
329
352
|
Simple enough, right? This is going to render a `:select` input for choosing the `:company`, and another
|
|
330
353
|
`:select` input with `:multiple` option for the `:roles`. You can, of course, change it to use radio
|
|
331
|
-
buttons and
|
|
354
|
+
buttons and check boxes as well:
|
|
332
355
|
|
|
333
356
|
```ruby
|
|
334
|
-
f.association :company, :
|
|
335
|
-
f.association :roles, :
|
|
357
|
+
f.association :company, as: :radio_buttons
|
|
358
|
+
f.association :roles, as: :check_boxes
|
|
336
359
|
```
|
|
337
360
|
|
|
338
361
|
The association helper just invokes `input` under the hood, so all options available to `:select`,
|
|
@@ -340,13 +363,13 @@ The association helper just invokes `input` under the hood, so all options avail
|
|
|
340
363
|
the collection by hand, all together with the prompt:
|
|
341
364
|
|
|
342
365
|
```ruby
|
|
343
|
-
f.association :company, :
|
|
366
|
+
f.association :company, collection: Company.active.all(order: 'name'), prompt: "Choose a Company"
|
|
344
367
|
```
|
|
345
368
|
|
|
346
369
|
In case you want to declare different labels and values:
|
|
347
370
|
|
|
348
371
|
```ruby
|
|
349
|
-
f.association :company, :
|
|
372
|
+
f.association :company, label_method: :company_name, value_method: :id, include_blank: false
|
|
350
373
|
```
|
|
351
374
|
|
|
352
375
|
### Buttons
|
|
@@ -369,7 +392,7 @@ calling input with a block like so:
|
|
|
369
392
|
|
|
370
393
|
```erb
|
|
371
394
|
<%= f.input :role do %>
|
|
372
|
-
<%= f.select :role, Role.all.map { |r| [r.name, r.id, { :
|
|
395
|
+
<%= f.select :role, Role.all.map { |r| [r.name, r.id, { class: r.company.id }] }, include_blank: true %>
|
|
373
396
|
<% end %>
|
|
374
397
|
```
|
|
375
398
|
|
|
@@ -415,7 +438,7 @@ end
|
|
|
415
438
|
|
|
416
439
|
#### Collection Check Boxes
|
|
417
440
|
|
|
418
|
-
Creates a collection of
|
|
441
|
+
Creates a collection of check boxes with labels associated (same API as `collection_select`):
|
|
419
442
|
|
|
420
443
|
```ruby
|
|
421
444
|
form_for @user do |f|
|
|
@@ -440,36 +463,36 @@ form_for @user do |f|
|
|
|
440
463
|
end
|
|
441
464
|
```
|
|
442
465
|
|
|
443
|
-
##
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
466
|
+
## Mappings/Inputs available
|
|
467
|
+
|
|
468
|
+
**SimpleForm** comes with a lot of default mappings:
|
|
469
|
+
|
|
470
|
+
```text
|
|
471
|
+
Mapping Input Column Type
|
|
472
|
+
|
|
473
|
+
boolean check box boolean
|
|
474
|
+
string text field string
|
|
475
|
+
email email field string with name matching "email"
|
|
476
|
+
url url field string with name matching "url"
|
|
477
|
+
tel tel field string with name matching "phone"
|
|
478
|
+
password password field string with name matching "password"
|
|
479
|
+
search search -
|
|
480
|
+
text text area text
|
|
481
|
+
file file field string, responding to file methods
|
|
482
|
+
hidden hidden field -
|
|
483
|
+
integer number field integer
|
|
484
|
+
float number field float
|
|
485
|
+
decimal number field decimal
|
|
486
|
+
range range field -
|
|
487
|
+
datetime datetime select datetime/timestamp
|
|
488
|
+
date date select date
|
|
489
|
+
time time select time
|
|
490
|
+
select collection select belongs_to/has_many/has_and_belongs_to_many associations
|
|
491
|
+
radio_buttons collection radio buttons belongs_to
|
|
492
|
+
check_boxes collection check boxes has_many/has_and_belongs_to_many associations
|
|
493
|
+
country country select string with name matching "country"
|
|
494
|
+
time_zone time zone select string with name matching "time_zone"
|
|
495
|
+
```
|
|
473
496
|
|
|
474
497
|
## Custom inputs
|
|
475
498
|
|
|
@@ -488,9 +511,8 @@ end
|
|
|
488
511
|
And use it in your views:
|
|
489
512
|
|
|
490
513
|
```ruby
|
|
491
|
-
f.input :money, :
|
|
514
|
+
f.input :money, as: :currency
|
|
492
515
|
```
|
|
493
|
-
Note, you may have to create the `app/inputs/` directory and restart your webserver.
|
|
494
516
|
|
|
495
517
|
You can also redefine existing **SimpleForm** inputs by creating a new class with the same name. For
|
|
496
518
|
instance, if you want to wrap date/time/datetime in a div, you can do:
|
|
@@ -524,7 +546,7 @@ Create a helper method that calls `simple_form_for` with a custom builder:
|
|
|
524
546
|
```ruby
|
|
525
547
|
def custom_form_for(object, *args, &block)
|
|
526
548
|
options = args.extract_options!
|
|
527
|
-
simple_form_for(object, *(args << options.merge(:
|
|
549
|
+
simple_form_for(object, *(args << options.merge(builder: CustomFormBuilder)), &block)
|
|
528
550
|
end
|
|
529
551
|
```
|
|
530
552
|
|
|
@@ -533,7 +555,7 @@ Create a form builder class that inherits from `SimpleForm::FormBuilder`.
|
|
|
533
555
|
```ruby
|
|
534
556
|
class CustomFormBuilder < SimpleForm::FormBuilder
|
|
535
557
|
def input(attribute_name, options = {}, &block)
|
|
536
|
-
options[:input_html].merge! :
|
|
558
|
+
options[:input_html].merge! class: 'custom'
|
|
537
559
|
super
|
|
538
560
|
end
|
|
539
561
|
end
|
|
@@ -616,7 +638,7 @@ that would post either `male` or `female` as value. With **SimpleForm** you coul
|
|
|
616
638
|
like this:
|
|
617
639
|
|
|
618
640
|
```ruby
|
|
619
|
-
f.input :gender, :
|
|
641
|
+
f.input :gender, collection: [:male, :female]
|
|
620
642
|
```
|
|
621
643
|
|
|
622
644
|
And **SimpleForm** will try a lookup like this in your locale file, to find the right labels to show:
|
|
@@ -678,7 +700,7 @@ en:
|
|
|
678
700
|
This difference exists because **SimpleForm** relies on `object_name` provided by Rails'
|
|
679
701
|
FormBuilder to determine the translation path for a given object instead of `i18n_key` from the
|
|
680
702
|
object itself. Thus, similarly, if a form for an `Admin::User` object is defined by calling
|
|
681
|
-
`simple_form_for @admin_user, :
|
|
703
|
+
`simple_form_for @admin_user, as: :some_user`, **SimpleForm** will look for translations
|
|
682
704
|
under `some_user` instead of `admin_user`.
|
|
683
705
|
|
|
684
706
|
## Configuration
|
|
@@ -694,8 +716,8 @@ With **SimpleForm** you can configure how your components will be rendered using
|
|
|
694
716
|
The syntax looks like this:
|
|
695
717
|
|
|
696
718
|
```ruby
|
|
697
|
-
config.wrappers :
|
|
698
|
-
:
|
|
719
|
+
config.wrappers tag: :div, class: :input,
|
|
720
|
+
error_class: :field_with_errors do |b|
|
|
699
721
|
|
|
700
722
|
# Form extensions
|
|
701
723
|
b.use :html5
|
|
@@ -706,8 +728,8 @@ config.wrappers :tag => :div, :class => :input,
|
|
|
706
728
|
|
|
707
729
|
# Form components
|
|
708
730
|
b.use :label_input
|
|
709
|
-
b.use :hint, :
|
|
710
|
-
b.use :error, :
|
|
731
|
+
b.use :hint, wrap_with: { tag: :span, class: :hint }
|
|
732
|
+
b.use :error, wrap_with: { tag: :span, class: :error }
|
|
711
733
|
end
|
|
712
734
|
```
|
|
713
735
|
|
|
@@ -730,9 +752,9 @@ You can create new _Form components_ using the wrappers API as in the following
|
|
|
730
752
|
config.wrappers do |b|
|
|
731
753
|
b.use :placeholder
|
|
732
754
|
b.use :label_input
|
|
733
|
-
b.wrapper :
|
|
734
|
-
component.use :hint, :
|
|
735
|
-
component.use :error, :
|
|
755
|
+
b.wrapper tag: :div, class: 'separator' do |component|
|
|
756
|
+
component.use :hint, wrap_with: { tag: :span, class: :hint }
|
|
757
|
+
component.use :error, wrap_with: { tag: :span, class: :error }
|
|
736
758
|
end
|
|
737
759
|
end
|
|
738
760
|
```
|
|
@@ -745,9 +767,9 @@ If you want to customize the custom _Form components_ on demand you can give it
|
|
|
745
767
|
config.wrappers do |b|
|
|
746
768
|
b.use :placeholder
|
|
747
769
|
b.use :label_input
|
|
748
|
-
b.wrapper :my_wrapper, :
|
|
749
|
-
component.use :hint, :
|
|
750
|
-
component.use :error, :
|
|
770
|
+
b.wrapper :my_wrapper, tag: :div, class: 'separator' do |component|
|
|
771
|
+
component.use :hint, wrap_with: { tag: :span, class: :hint }
|
|
772
|
+
component.use :error, wrap_with: { tag: :span, class: :error }
|
|
751
773
|
end
|
|
752
774
|
end
|
|
753
775
|
```
|
|
@@ -756,13 +778,13 @@ and now you can pass options to your `input` calls to customize the `:my_wrapper
|
|
|
756
778
|
|
|
757
779
|
```ruby
|
|
758
780
|
# Completely turns off the custom wrapper
|
|
759
|
-
f.input :name, :
|
|
781
|
+
f.input :name, my_wrapper: false
|
|
760
782
|
|
|
761
783
|
# Configure the html
|
|
762
|
-
f.input :name, :
|
|
784
|
+
f.input :name, my_wrapper_html: { id: 'special_id' }
|
|
763
785
|
|
|
764
786
|
# Configure the tag
|
|
765
|
-
f.input :name, :
|
|
787
|
+
f.input :name, my_wrapper_tag: :p
|
|
766
788
|
```
|
|
767
789
|
|
|
768
790
|
You can also define more than one wrapper and pick one to render in a specific form or input.
|
|
@@ -779,33 +801,33 @@ and use it in this way:
|
|
|
779
801
|
|
|
780
802
|
```ruby
|
|
781
803
|
# Specifying to whole form
|
|
782
|
-
simple_form_for @user, :
|
|
804
|
+
simple_form_for @user, wrapper: :small do |f|
|
|
783
805
|
f.input :name
|
|
784
806
|
end
|
|
785
807
|
|
|
786
808
|
# Specifying to one input
|
|
787
809
|
simple_form_for @user do |f|
|
|
788
|
-
f.input :name, :
|
|
810
|
+
f.input :name, wrapper: :small
|
|
789
811
|
end
|
|
790
812
|
```
|
|
791
813
|
|
|
792
814
|
**SimpleForm** also allows you to use optional elements. For instance, let's suppose you want to use
|
|
793
815
|
hints or placeholders, but you don't want them to be generated automatically. You can set their
|
|
794
|
-
default values to `false` or use the `optional` method. Is
|
|
816
|
+
default values to `false` or use the `optional` method. Is preferable to use the `optional` syntax:
|
|
795
817
|
|
|
796
818
|
```ruby
|
|
797
|
-
config.wrappers :
|
|
819
|
+
config.wrappers placeholder: false do |b|
|
|
798
820
|
b.use :placeholder
|
|
799
821
|
b.use :label_input
|
|
800
|
-
b.wrapper :
|
|
801
|
-
component.optional :hint, :
|
|
802
|
-
component.use :error, :
|
|
822
|
+
b.wrapper tag: :div, class: 'separator' do |component|
|
|
823
|
+
component.optional :hint, wrap_with: { tag: :span, class: :hint }
|
|
824
|
+
component.use :error, wrap_with: { tag: :span, class: :error }
|
|
803
825
|
end
|
|
804
826
|
end
|
|
805
827
|
```
|
|
806
828
|
|
|
807
|
-
By setting it as `optional`, a hint will only be generated when
|
|
808
|
-
The same for
|
|
829
|
+
By setting it as `optional`, a hint will only be generated when `hint: true` is explicitly used.
|
|
830
|
+
The same for placeholder.
|
|
809
831
|
|
|
810
832
|
## HTML 5 Notice
|
|
811
833
|
|
|
@@ -839,7 +861,7 @@ help you to use some generic javascript validation.
|
|
|
839
861
|
You can also add `novalidate` to a specific form by setting the option on the form itself:
|
|
840
862
|
|
|
841
863
|
```erb
|
|
842
|
-
<%= simple_form_for(resource, :
|
|
864
|
+
<%= simple_form_for(resource, html: {novalidate: true}) do |form| %>
|
|
843
865
|
```
|
|
844
866
|
|
|
845
867
|
Please notice that any of the configurations above will disable the `placeholder` component,
|
|
@@ -884,3 +906,5 @@ https://github.com/plataformatec/simple_form/issues
|
|
|
884
906
|
## License
|
|
885
907
|
|
|
886
908
|
MIT License. Copyright 2009-2013 Plataformatec. http://plataformatec.com.br
|
|
909
|
+
|
|
910
|
+
You are not granted rights or licenses to the trademarks of the Plataformatec, including without limitation the Simple Form name or logo.
|