ehoch_simple_form 2.0.2.dev
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +257 -0
- data/MIT-LICENSE +20 -0
- data/README.md +797 -0
- data/lib/generators/simple_form/USAGE +3 -0
- data/lib/generators/simple_form/install_generator.rb +32 -0
- data/lib/generators/simple_form/templates/README +12 -0
- data/lib/generators/simple_form/templates/_form.html.erb +13 -0
- data/lib/generators/simple_form/templates/_form.html.haml +10 -0
- data/lib/generators/simple_form/templates/_form.html.slim +10 -0
- data/lib/generators/simple_form/templates/config/initializers/simple_form.rb.tt +181 -0
- data/lib/generators/simple_form/templates/config/locales/simple_form.en.yml +26 -0
- data/lib/simple_form.rb +215 -0
- data/lib/simple_form/action_view_extensions/builder.rb +338 -0
- data/lib/simple_form/action_view_extensions/form_helper.rb +74 -0
- data/lib/simple_form/components.rb +20 -0
- data/lib/simple_form/components/errors.rb +35 -0
- data/lib/simple_form/components/hints.rb +18 -0
- data/lib/simple_form/components/html5.rb +26 -0
- data/lib/simple_form/components/label_input.rb +15 -0
- data/lib/simple_form/components/labels.rb +79 -0
- data/lib/simple_form/components/maxlength.rb +41 -0
- data/lib/simple_form/components/min_max.rb +50 -0
- data/lib/simple_form/components/pattern.rb +34 -0
- data/lib/simple_form/components/placeholders.rb +16 -0
- data/lib/simple_form/components/readonly.rb +22 -0
- data/lib/simple_form/core_ext/hash.rb +16 -0
- data/lib/simple_form/error_notification.rb +48 -0
- data/lib/simple_form/form_builder.rb +472 -0
- data/lib/simple_form/helpers.rb +12 -0
- data/lib/simple_form/helpers/autofocus.rb +11 -0
- data/lib/simple_form/helpers/disabled.rb +15 -0
- data/lib/simple_form/helpers/readonly.rb +15 -0
- data/lib/simple_form/helpers/required.rb +35 -0
- data/lib/simple_form/helpers/validators.rb +44 -0
- data/lib/simple_form/i18n_cache.rb +22 -0
- data/lib/simple_form/inputs.rb +21 -0
- data/lib/simple_form/inputs/base.rb +162 -0
- data/lib/simple_form/inputs/block_input.rb +14 -0
- data/lib/simple_form/inputs/boolean_input.rb +64 -0
- data/lib/simple_form/inputs/collection_check_boxes_input.rb +21 -0
- data/lib/simple_form/inputs/collection_input.rb +101 -0
- data/lib/simple_form/inputs/collection_radio_buttons_input.rb +63 -0
- data/lib/simple_form/inputs/collection_select_input.rb +14 -0
- data/lib/simple_form/inputs/date_time_input.rb +28 -0
- data/lib/simple_form/inputs/file_input.rb +9 -0
- data/lib/simple_form/inputs/grouped_collection_select_input.rb +41 -0
- data/lib/simple_form/inputs/hidden_input.rb +17 -0
- data/lib/simple_form/inputs/numeric_input.rb +24 -0
- data/lib/simple_form/inputs/password_input.rb +12 -0
- data/lib/simple_form/inputs/priority_input.rb +24 -0
- data/lib/simple_form/inputs/range_input.rb +14 -0
- data/lib/simple_form/inputs/string_input.rb +23 -0
- data/lib/simple_form/inputs/text_input.rb +11 -0
- data/lib/simple_form/map_type.rb +16 -0
- data/lib/simple_form/version.rb +3 -0
- data/lib/simple_form/wrappers.rb +8 -0
- data/lib/simple_form/wrappers/builder.rb +103 -0
- data/lib/simple_form/wrappers/many.rb +69 -0
- data/lib/simple_form/wrappers/root.rb +34 -0
- data/lib/simple_form/wrappers/single.rb +18 -0
- data/test/action_view_extensions/builder_test.rb +577 -0
- data/test/action_view_extensions/form_helper_test.rb +104 -0
- data/test/components/label_test.rb +310 -0
- data/test/form_builder/association_test.rb +177 -0
- data/test/form_builder/button_test.rb +47 -0
- data/test/form_builder/error_notification_test.rb +79 -0
- data/test/form_builder/error_test.rb +121 -0
- data/test/form_builder/general_test.rb +356 -0
- data/test/form_builder/hint_test.rb +139 -0
- data/test/form_builder/input_field_test.rb +63 -0
- data/test/form_builder/label_test.rb +71 -0
- data/test/form_builder/wrapper_test.rb +149 -0
- data/test/generators/simple_form_generator_test.rb +32 -0
- data/test/inputs/boolean_input_test.rb +108 -0
- data/test/inputs/collection_check_boxes_input_test.rb +224 -0
- data/test/inputs/collection_radio_buttons_input_test.rb +326 -0
- data/test/inputs/collection_select_input_test.rb +241 -0
- data/test/inputs/datetime_input_test.rb +99 -0
- data/test/inputs/disabled_test.rb +38 -0
- data/test/inputs/discovery_test.rb +61 -0
- data/test/inputs/file_input_test.rb +16 -0
- data/test/inputs/general_test.rb +69 -0
- data/test/inputs/grouped_collection_select_input_test.rb +118 -0
- data/test/inputs/hidden_input_test.rb +30 -0
- data/test/inputs/numeric_input_test.rb +173 -0
- data/test/inputs/priority_input_test.rb +43 -0
- data/test/inputs/readonly_test.rb +61 -0
- data/test/inputs/required_test.rb +113 -0
- data/test/inputs/string_input_test.rb +140 -0
- data/test/inputs/text_input_test.rb +24 -0
- data/test/simple_form_test.rb +9 -0
- data/test/support/discovery_inputs.rb +21 -0
- data/test/support/misc_helpers.rb +102 -0
- data/test/support/mock_controller.rb +24 -0
- data/test/support/models.rb +210 -0
- data/test/test_helper.rb +90 -0
- metadata +210 -0
data/CHANGELOG.md
ADDED
@@ -0,0 +1,257 @@
|
|
1
|
+
## 2.0.2 (Not released yet)
|
2
|
+
|
3
|
+
### enhancements
|
4
|
+
* Add `:inline_label` configuration to nested booleans to display text inline with checkbox. ([@ehoch](https://github.com/ehoch))
|
5
|
+
* Add html support for hints ([@findrails](https://github.com/findrails))
|
6
|
+
|
7
|
+
### bug fix
|
8
|
+
* Fix `min_max` component to not output maximum value. ([@julian7](https://github.com/julian7)).
|
9
|
+
Closes [#483](https://github.com/plataformatec/simple_form/issues/483)
|
10
|
+
* Remove leading and trailing whitespace from `label_text`
|
11
|
+
Closes [#492](https://github.com/plataformatec/simple_form/issues/492)
|
12
|
+
* Fix checked radio button issue when value is false ([@georgehemmings](https://github.com/georgehemmings)).
|
13
|
+
|
14
|
+
## 2.0.1
|
15
|
+
|
16
|
+
### bug fix
|
17
|
+
* Sanitaze html attributes to `label` method. ([@nashby](https://github.com/nashby)).
|
18
|
+
Closes [#472](https://github.com/plataformatec/simple_form/issues/472)
|
19
|
+
* Make `collection_check_boxes` and `collection_radio_buttons` work with local variables.
|
20
|
+
Closes [#474](https://github.com/plataformatec/simple_form/issues/474)
|
21
|
+
* Use `html5` component by default in the bootstrap generator. ([@isc](https://github.com/isc)).
|
22
|
+
Closes [#471](https://github.com/plataformatec/simple_form/issues/471)
|
23
|
+
|
24
|
+
## 2.0.0
|
25
|
+
|
26
|
+
### enhancements
|
27
|
+
* Add `button_class` configuration to change the class of buttons. ([@sryche](https://github.com/sryche))
|
28
|
+
* Add `disabled` class to a disabled input.
|
29
|
+
* Generate configuration file with `browser_validations` disabled.
|
30
|
+
* Add option and configuration to specify the collection wrapper class. ([@mfila](https://github.com/mfila))
|
31
|
+
* Add proc support to `collection` option. ([@jeffkreeftmeijer](https://github.com/jeffkreeftmeijer))
|
32
|
+
* `simple_form_for` allows default options for its inputs `:defaults => {}`.
|
33
|
+
* Add `readonly` as option of input method. ([@Untainted123](https://github.com/Untainted123))
|
34
|
+
* `simple_fields_for` for inherits wrapper option form the form builder. ([@nashby](https://github.com/nashby))
|
35
|
+
* Use action prefix in the form css class. Closes [#360](https://github.com/plataformatec/simple_form/issues/360).
|
36
|
+
This is not backward compatible with the previous versions of SimpleForm.
|
37
|
+
For more informations see [this comment](https://github.com/plataformatec/simple_form/issues/360#issuecomment-3000780).
|
38
|
+
([@nashby](https://github.com/nashby))
|
39
|
+
* Add a readonly component that does automatically readonly lookup from object
|
40
|
+
* Add support for proc or lambda as option for format validator ([@nashby](https://github.com/nashby))
|
41
|
+
* Handle validates_length_of :is option in maxlength ([@nashby](https://github.com/nashby))
|
42
|
+
* Add field_with_hint css class to the wrapper when the input has a hint, similar to field_with_errors ([@nashby](https://github.com/nashby))
|
43
|
+
* Add :grouped_select input type, mapping to Rails grouped_collection_select helper ([@semaperepelitsa](https://github.com/semaperepelitsa))
|
44
|
+
* Add automatic translation of options for collection inputs given a collection of symbols ([@klobuczek](https://github.com/klobuczek))
|
45
|
+
* Add `:boolean_style` config to change how check boxes and radios will be displayed.
|
46
|
+
Options are `:inline = input + label` (default) and `:nested = label > input`.
|
47
|
+
* Add possibility to give a block to `collection_radio` and `collection_check_boxes`,
|
48
|
+
yielding a custom builder to generate custom label and input structure. It
|
49
|
+
is used internally with the :nested option for `:boolean_style`, and is useful
|
50
|
+
to allow some more customization if required.
|
51
|
+
* Do not generate hidden check box field when using nested boolean style, as it is considered
|
52
|
+
invalid markup in HTML5. This will work by default in Rails > 3.2.1 (not released at this time),
|
53
|
+
and is backported inside SimpleForm builder extensions.
|
54
|
+
More info in [#215](https://github.com/plataformatec/simple_form/issues/215)
|
55
|
+
* Add `item_wrapper_class` configuration option for collection radio buttons / check boxes inputs.
|
56
|
+
* Change default generator templates to use .form-inputs and .form-actions classes in wrapper divs.
|
57
|
+
(the latter is the default in bootstrap, so this makes it easier to integrate).
|
58
|
+
* Field error now accepts HTML tags ([@edison](https://github.com/edison))
|
59
|
+
* Add `generate_additional_classes_for` config option to selectively disable extra
|
60
|
+
css classes for components - wrapper, label and input. ([krzyzak](https://github.com/krzyzak))
|
61
|
+
|
62
|
+
### deprecation
|
63
|
+
* Deprecate part of the old configuration API in favor of the wrapper API which allows you to customize your inputs
|
64
|
+
in a more flexible way. See [this guide](https://github.com/plataformatec/simple_form/wiki/Upgrading-to-Simple-Form-2.0)
|
65
|
+
to know how upgrade.
|
66
|
+
* Deprecate the `translate` configuration in favor of `translate_labels`
|
67
|
+
* Deprecate the `html5` configuration in favor of a new `html5` component
|
68
|
+
* Deprecate `:radio` input type in favor of `:radio_buttons`
|
69
|
+
* Deprecate `collection_radio` form helper in favor of `collection_radio_buttons`
|
70
|
+
(the label class has changed as well)
|
71
|
+
* Remove `error_notification_id` configuration
|
72
|
+
|
73
|
+
### bug fix
|
74
|
+
* Fix i18n lookup with attributes with same name of models.
|
75
|
+
Closes [#149](https://github.com/plataformatec/simple_form/issues/149)
|
76
|
+
and [#364](https://github.com/plataformatec/simple_form/issues/364).
|
77
|
+
([@nashby](https://github.com/nashby) and [@MarceloCajueiro](https://github.com/MarceloCajueiro))
|
78
|
+
* Do not generate `for` attribute for the collection label when it is a checkbox or radio.
|
79
|
+
Closes [#344](https://github.com/plataformatec/simple_form/issues/344).
|
80
|
+
([@nashby](https://github.com/nashby) and [@mfila](https://github.com/mfila))
|
81
|
+
* Select can have required option when the `:include_blank` option is passed.
|
82
|
+
Closes [#340](https://github.com/plataformatec/simple_form/issues/340). ([@nashby](https://github.com/nashby))
|
83
|
+
* `:checked` option should override the existing associations on `collection_check_boxes`.
|
84
|
+
Closes [#341](https://github.com/plataformatec/simple_form/issues/341). ([@nashby](https://github.com/nashby))
|
85
|
+
* Move default attribute translations out of root - use "defaults" key instead
|
86
|
+
Closes [#384](https://github.com/plataformatec/simple_form/issues/384). ([@fringd](https://github.com/fringd))
|
87
|
+
* Fix label to datetime inputs to point to first select. ([@georgehemmings](https://github.com/georgehemmings))
|
88
|
+
* Fix usage of f.button :button with Rails 3.2.
|
89
|
+
Closes [#449](https://github.com/plataformatec/simple_form/issues/449).
|
90
|
+
|
91
|
+
## 1.5.2
|
92
|
+
|
93
|
+
### bug fix
|
94
|
+
* Remove the internal usage of deprecated `:components`
|
95
|
+
|
96
|
+
## 1.5.1
|
97
|
+
|
98
|
+
### deprecation
|
99
|
+
* `:components` options is now deprecated
|
100
|
+
|
101
|
+
### bug fix
|
102
|
+
* Fallback to default label when block is provided. ([@pivotal-casebook](https://github.com/pivotal-casebook))
|
103
|
+
* Do not override default selection through attribute value in collection select when label/value methods are lambdas.
|
104
|
+
|
105
|
+
## 1.5.0
|
106
|
+
|
107
|
+
### enhancements
|
108
|
+
* Simplified generator by using directory action. ([@rupert654](https://github.com/rupert654))
|
109
|
+
* Support for `maxlength` on string inputs inferred from validation. ([@srbartlett](https://github.com/srbartlett))
|
110
|
+
* Change form css class handling to only add the dom class when one is not given to the form call.
|
111
|
+
([@patrick99e99](https://github.com/patrick99e99))
|
112
|
+
* Support for required attributes when action validations are present. ([@csegura](http://github.com/csegura))
|
113
|
+
* Do not generate `size` attribute for numeric input. ([@csegura](https://github.com/jasonmp85))
|
114
|
+
* Support for `maxlength` on text area inputs inferred from validation.
|
115
|
+
* Support for `pattern` on text field inferred from validation when `:pattern` is true.
|
116
|
+
* Break Text, Password and File into their own inputs.
|
117
|
+
* Support easy enabling and disabling of components for specific inputs.
|
118
|
+
* Add HTML5 range input.
|
119
|
+
|
120
|
+
### bug fix
|
121
|
+
* Fix bug when `simple_fields_for` is used with a hash like models and Rails 3.1.
|
122
|
+
* Fix bug that does not remove the `:item_wrapper_tag` or the `:collection_wrapper_tag` on collection
|
123
|
+
inputs when nil or false value is passed to these options. ([@dw2](https://gitbub.com/dw2))
|
124
|
+
* Fix bug that disable the entire select and wrapper when `disabled` option is a string or array.
|
125
|
+
* Fix bug when using label/value methods as procs together with disabled/selected options as procs for select inputs.
|
126
|
+
|
127
|
+
## 1.4.2
|
128
|
+
|
129
|
+
### enhancements
|
130
|
+
* Rails 3.1 support.
|
131
|
+
|
132
|
+
## 1.4.1
|
133
|
+
|
134
|
+
### enhancements
|
135
|
+
* ignore required attribute when conditional validations are present.
|
136
|
+
|
137
|
+
### bug fix
|
138
|
+
* Do not use `required='required'` when browser validations are turned off.
|
139
|
+
* Sanitize HMTL attributes in error and hint helpers when options are present.
|
140
|
+
* Improve i18n lookup by ignoring explicit child index given to form builder.
|
141
|
+
(tests by [@rywall](https://github.com/rywall))
|
142
|
+
* Fixes the form specific validation option if specified on the form itself. ([@medihack](https://github.com/medihack))
|
143
|
+
|
144
|
+
## 1.4.0
|
145
|
+
|
146
|
+
### enhancements
|
147
|
+
* Add label class configuration option. ([@reu](http://github.com/reu))
|
148
|
+
* Improve i18n lookup (labels/hints/placeholders) for nested models.
|
149
|
+
* Use the given custom builder with `simple_fields_for`. ([@giniedp](https://github.com/giniedp))
|
150
|
+
* Add slim form generator. ([@fagiani](https://github.com/fagiani))
|
151
|
+
* Add `form_class` configuration option. ([@fagiani](https://github.com/fagiani))
|
152
|
+
* Default step of `any` for number input with non integer attributes. ([@fedesoria](https://github.com/fedesoria))
|
153
|
+
* Add option to disable HTML5 browser validations on all forms. ([@coryschires](https://github.com/coryschires))
|
154
|
+
* Add option to disable all HTML5 extensions. ([@wolframarnold](https://github.com/wolframarnold))
|
155
|
+
* Add `input_field` helper to form builder. ([@jeroenhouben](https://github.com/jeroenhouben))
|
156
|
+
* Allow inputs to be discovered on demand by placing them at app/inputs (a la formtastic).
|
157
|
+
* Add `full_error` on that shows the error with the attribute name.
|
158
|
+
|
159
|
+
### bug fix
|
160
|
+
* Fix for file attributes automatic detection, to work with virtual attributes.
|
161
|
+
* Fix for numeric fields validation options using symbols and procs.
|
162
|
+
* Fix password attributes to add `size` and `maxlength` options the same way as string. ([@fedesoria](https://github.com/fedesoria))
|
163
|
+
* Fix bug with custom form builders and new mappings being added to the superclass builder. ([@rdvdijk](https://github.com/rdvdijk))
|
164
|
+
* Fix HTML validation issue with `collection_check_boxes`.
|
165
|
+
|
166
|
+
## 1.3.1
|
167
|
+
|
168
|
+
### enhancements
|
169
|
+
* Add `:autofocus` HTML5 attribute support. ([@jpzwarte](https://github.com/jpzwarte))
|
170
|
+
* Add possibility to specify custom builder and inherit mappings. ([@rejeep](https://github.com/rejeep))
|
171
|
+
* Make custom mappings work with all attributes types. ([@rafaelfranca](https://github.com/rafaelfranca))
|
172
|
+
* Add support for procs/lambdas in text/value methods for `collection_select`.
|
173
|
+
|
174
|
+
### deprecation
|
175
|
+
* removed the deprecated `:remote_form_for`
|
176
|
+
|
177
|
+
### bug fix
|
178
|
+
* Only add the `required` HTML 5 attribute for valid inputs, disable in selects (not allowed).
|
179
|
+
* Fix error when using hints without an attribute.
|
180
|
+
([@butsjoh](https://github.com/butsjoh) and [@rafaelfranca](https://github.com/rafaelfranca))
|
181
|
+
* Fix messy html output for hint, error and label components.
|
182
|
+
([@butsjoh](https://github.com/butsjoh) and [@rafaelfranca](https://github.com/rafaelfranca))
|
183
|
+
* Allow direct setting of for attribute on label. ([@Bertg](https://github.com/Bertg))
|
184
|
+
|
185
|
+
## 1.3.0
|
186
|
+
|
187
|
+
### enhancements
|
188
|
+
* Allow collection input to accept a collection of symbols.
|
189
|
+
* Add default css class to button.
|
190
|
+
* Allow forms for objects that do not respond to the `errors` method.
|
191
|
+
* `collection_check_boxes` and `collection_radio` now wrap the input in the label.
|
192
|
+
* Automatic add min/max values for numeric attributes based on validations and step for integers - HTML5.
|
193
|
+
([@dasch](https://github.com/dasch))
|
194
|
+
* Add `:placeholder` option for string inputs, allowing customization through I18n - HTML5.
|
195
|
+
([@jonathan](https://github.com/jonathan))
|
196
|
+
* Add `:search` and `:tel` input types, with `:tel` mapping automatically from attributes matching "phone" - HTML5.
|
197
|
+
* Add `:required` html attribute for required inputs - HTML5.
|
198
|
+
* Add optional `:components` option to input to control component rendering. ([@khoan](https://github.com/khoan))
|
199
|
+
* Add `SimpleForm.translate` as an easy way to turn off SimpleForm internal translations.
|
200
|
+
* Add `:disabled` option for all inputs. ([@fabiob](https://github.com/fabiob))
|
201
|
+
* Add collection wrapper tag and item wrapper tag to wrap elements in collection helpers - radio / check boxes.
|
202
|
+
* Add `SimpleForm.input_mappings` to allow configuring custom mappings for inputs. ([@TMaYaD](https://github.com/TMaYaD))
|
203
|
+
|
204
|
+
### bug fix
|
205
|
+
* Search for validations on both association and attribute.
|
206
|
+
* Use `controller.action_name` to lookup action only when available, to fix issue with Rspec views tests.
|
207
|
+
([@rafaelfranca](https://github.com/rafaelfranca))
|
208
|
+
|
209
|
+
## 1.2.2
|
210
|
+
|
211
|
+
### enhancements
|
212
|
+
* Compatibility with Rails 3 RC.
|
213
|
+
|
214
|
+
## 1.2.1
|
215
|
+
|
216
|
+
### enhancements
|
217
|
+
* Added haml generator support. ([@grimen](https://github.com/grimen))
|
218
|
+
* Added `error_notification` message to form builder.
|
219
|
+
* Added required by default as configuration option.
|
220
|
+
* Added `label_input` as component, allowing boolean to change its order (input appearing first than label).
|
221
|
+
* Added `error_method` to tidy up how errors are exhibited.
|
222
|
+
* Added error class on wrappers. ([@jduff](https://github.com/jduff))
|
223
|
+
* Changed numeric types to have `type=number` for HTML5.
|
224
|
+
|
225
|
+
## 1.2.0
|
226
|
+
|
227
|
+
### deprecation
|
228
|
+
* Changed `simple_form_install` generator to `simple_form:install`.
|
229
|
+
|
230
|
+
### enhancements
|
231
|
+
* Added support to presence validation to check if attribute is required or not. ([@gcirne](https://github.com/gcirne))
|
232
|
+
* Added `input` as class to wrapper tag.
|
233
|
+
* Added config options for hint and error tags. ([@tjogin](https://github.com/tjogin))
|
234
|
+
|
235
|
+
## 1.1.3
|
236
|
+
|
237
|
+
### deprecation
|
238
|
+
* removed `:conditions`, `:order`, `:joins` and `:include` support in `f.association`.
|
239
|
+
|
240
|
+
## 1.1.2
|
241
|
+
|
242
|
+
### bug fix
|
243
|
+
* Ensure type is set to "text" and not "string".
|
244
|
+
|
245
|
+
## 1.1.1
|
246
|
+
|
247
|
+
### bug fix
|
248
|
+
* Fix some escaping issues.
|
249
|
+
|
250
|
+
## 1.1.0
|
251
|
+
|
252
|
+
### enhancements
|
253
|
+
* Rails 3 support with generators, templates and HTML 5.
|
254
|
+
|
255
|
+
## 1.0
|
256
|
+
|
257
|
+
* First release.
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 PlataformaTec http://blog.plataformatec.com.br/
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,797 @@
|
|
1
|
+
# SimpleForm - Rails forms made easy.
|
2
|
+
[![Build Status](https://secure.travis-ci.org/plataformatec/simple_form.png)](http://travis-ci.org/plataformatec/simple_form)
|
3
|
+
|
4
|
+
**SimpleForm** aims to be as flexible as possible while helping you with powerful components to create
|
5
|
+
your forms. The basic goal of SimpleForm is to not touch your way of defining the layout, letting
|
6
|
+
you find the better design for your eyes. Most of the DSL was inherited from Formtastic,
|
7
|
+
which we are thankful for and should make you feel right at home.
|
8
|
+
|
9
|
+
INFO: This README is [also available in a friendly navigable format](http://simple-form.plataformatec.com.br/)
|
10
|
+
and refers to **SimpleForm** 2.0. If you are using **SimpleForm** in the versions 1.x, you should
|
11
|
+
check this branch:
|
12
|
+
|
13
|
+
https://github.com/plataformatec/simple_form/tree/v1.5
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add it to your Gemfile:
|
18
|
+
|
19
|
+
`gem 'simple_form'`
|
20
|
+
|
21
|
+
Run the following command to install it:
|
22
|
+
|
23
|
+
`bundle install`
|
24
|
+
|
25
|
+
Run the generator:
|
26
|
+
|
27
|
+
`rails generate simple_form:install`
|
28
|
+
|
29
|
+
Also, if you want to use the country select, you will need the
|
30
|
+
[country_select gem](https://rubygems.org/gems/country_select), add it to your Gemfile:
|
31
|
+
|
32
|
+
`gem 'country_select'`
|
33
|
+
|
34
|
+
### Twitter Bootstrap
|
35
|
+
|
36
|
+
**SimpleForm** 2.0 can be easily integrated to the [Twitter Bootstrap](http://twitter.github.com/bootstrap).
|
37
|
+
To do that you have to use the `bootstrap` option in the install generator, like this:
|
38
|
+
|
39
|
+
`rails generate simple_form:install --bootstrap`
|
40
|
+
|
41
|
+
You have to be sure that you added a copy of the [Twitter Bootstrap](http://twitter.github.com/bootstrap)
|
42
|
+
assets on your application.
|
43
|
+
|
44
|
+
For more information see the generator output, our
|
45
|
+
[example application code](https://github.com/rafaelfranca/simple_form-bootstrap) and
|
46
|
+
[the live example app](http://simple-form-bootstrap.plataformatec.com.br/).
|
47
|
+
|
48
|
+
**NOTE**: **SimpleForm** integration requires Twitter Bootstrap version 2.0 or higher.
|
49
|
+
|
50
|
+
## Usage
|
51
|
+
|
52
|
+
**SimpleForm** was designed to be customized as you need to. Basically it's a stack of components that
|
53
|
+
are invoked to create a complete html input for you, which by default contains label, hints, errors
|
54
|
+
and the input itself. It does not aim to create a lot of different logic from the default Rails
|
55
|
+
form helpers, as they do a great work by themselves. Instead, **SimpleForm** acts as a DSL and just
|
56
|
+
maps your input type (retrieved from the column definition in the database) to an specific helper method.
|
57
|
+
|
58
|
+
To start using **SimpleForm** you just have to use the helper it provides:
|
59
|
+
|
60
|
+
```erb
|
61
|
+
<%= simple_form_for @user do |f| %>
|
62
|
+
<%= f.input :username %>
|
63
|
+
<%= f.input :password %>
|
64
|
+
<%= f.button :submit %>
|
65
|
+
<% end %>
|
66
|
+
```
|
67
|
+
|
68
|
+
This will generate an entire form with labels for user name and password as well, and render errors
|
69
|
+
by default when you render the form with invalid data (after submitting for example).
|
70
|
+
|
71
|
+
You can overwrite the default label by passing it to the input method. You can also add a hint or
|
72
|
+
even a placeholder:
|
73
|
+
|
74
|
+
```erb
|
75
|
+
<%= simple_form_for @user do |f| %>
|
76
|
+
<%= f.input :username, :label => 'Your username please' %>
|
77
|
+
<%= f.input :password, :hint => 'No special characters.' %>
|
78
|
+
<%= f.input :email, :placeholder => 'user@domain.com' %>
|
79
|
+
<%= f.button :submit %>
|
80
|
+
<% end %>
|
81
|
+
```
|
82
|
+
|
83
|
+
In some cases you may want to disable labels, hints or error. Or you may want to configure the html
|
84
|
+
of any of them:
|
85
|
+
|
86
|
+
```erb
|
87
|
+
<%= simple_form_for @user do |f| %>
|
88
|
+
<%= f.input :username, :label_html => { :class => 'my_class' } %>
|
89
|
+
<%= f.input :password, :hint => false, :error_html => { :id => 'password_error'} %>
|
90
|
+
<%= f.input :password_confirmation, :label => false %>
|
91
|
+
<%= f.button :submit %>
|
92
|
+
<% end %>
|
93
|
+
```
|
94
|
+
|
95
|
+
It is also possible to pass any html attribute straight to the input, by using the `:input_html`
|
96
|
+
option, for instance:
|
97
|
+
|
98
|
+
```erb
|
99
|
+
<%= simple_form_for @user do |f| %>
|
100
|
+
<%= f.input :username, :input_html => { :class => 'special' } %>
|
101
|
+
<%= f.input :password, :input_html => { :maxlength => 20 } %>
|
102
|
+
<%= f.input :remember_me, :input_html => { :value => '1' } %>
|
103
|
+
<%= f.button :submit %>
|
104
|
+
<% end %>
|
105
|
+
```
|
106
|
+
|
107
|
+
If you want to pass the same options to all inputs in the form (for example, a default class),
|
108
|
+
you can use the `:defaults` option in `simple_form_for`. Specific options in `input` call will
|
109
|
+
overwrite the defaults:
|
110
|
+
|
111
|
+
```erb
|
112
|
+
<%= simple_form_for @user, :defaults => { :input_html => { :class => 'default_class' } } do |f| %>
|
113
|
+
<%= f.input :username, :input_html => { :class => 'special' } %>
|
114
|
+
<%= f.input :password, :input_html => { :maxlength => 20 } %>
|
115
|
+
<%= f.input :remember_me, :input_html => { :value => '1' } %>
|
116
|
+
<%= f.button :submit %>
|
117
|
+
<% end %>
|
118
|
+
```
|
119
|
+
|
120
|
+
Since **SimpleForm** generates a wrapper div around your label and input by default, you can pass
|
121
|
+
any html attribute to that wrapper as well using the `:wrapper_html` option, like so:
|
122
|
+
|
123
|
+
```erb
|
124
|
+
<%= simple_form_for @user do |f| %>
|
125
|
+
<%= f.input :username, :wrapper_html => { :class => 'username' } %>
|
126
|
+
<%= f.input :password, :wrapper_html => { :id => 'password' } %>
|
127
|
+
<%= f.input :remember_me, :wrapper_html => { :class => 'options' } %>
|
128
|
+
<%= f.button :submit %>
|
129
|
+
<% end %>
|
130
|
+
```
|
131
|
+
|
132
|
+
By default all inputs are required, which means an * is prepended to the label, but you can disable
|
133
|
+
it in any input you want:
|
134
|
+
|
135
|
+
```erb
|
136
|
+
<%= simple_form_for @user do |f| %>
|
137
|
+
<%= f.input :name, :required => false %>
|
138
|
+
<%= f.input :username %>
|
139
|
+
<%= f.input :password %>
|
140
|
+
<%= f.button :submit %>
|
141
|
+
<% end %>
|
142
|
+
```
|
143
|
+
|
144
|
+
**SimpleForm** also lets you overwrite the default input type it creates:
|
145
|
+
|
146
|
+
```erb
|
147
|
+
<%= simple_form_for @user do |f| %>
|
148
|
+
<%= f.input :username %>
|
149
|
+
<%= f.input :password %>
|
150
|
+
<%= f.input :description, :as => :text %>
|
151
|
+
<%= f.input :accepts, :as => :radio_buttons %>
|
152
|
+
<%= f.button :submit %>
|
153
|
+
<% end %>
|
154
|
+
```
|
155
|
+
|
156
|
+
So instead of a checkbox for the *accepts* attribute, you'll have a pair of radio buttons with yes/no
|
157
|
+
labels and a text area instead of a text field for the description. You can also render boolean
|
158
|
+
attributes using `:as => :select` to show a dropdown.
|
159
|
+
|
160
|
+
It is also possible to give the `:disabled` option to **SimpleForm**, and it'll automatically mark
|
161
|
+
the wrapper as disabled with a css class, so you can style labels, hints and other components inside
|
162
|
+
the wrapper as well:
|
163
|
+
|
164
|
+
```erb
|
165
|
+
<%= simple_form_for @user do |f| %>
|
166
|
+
<%= f.input :username, :disabled => true, :hint => 'You cannot change your username.' %>
|
167
|
+
<%= f.button :submit %>
|
168
|
+
<% end %>
|
169
|
+
```
|
170
|
+
|
171
|
+
**SimpleForm** accepts same options as their corresponding input type helper in Rails:
|
172
|
+
|
173
|
+
```erb
|
174
|
+
<%= simple_form_for @user do |f| %>
|
175
|
+
<%= f.input :date_of_birth, :as => :date, :start_year => Date.today.year - 90,
|
176
|
+
:end_year => Date.today.year - 12, :discard_day => true,
|
177
|
+
:order => [:month, :year] %>
|
178
|
+
<%= f.button :submit %>
|
179
|
+
<% end %>
|
180
|
+
```
|
181
|
+
|
182
|
+
**SimpleForm** also allows you to use label, hint, input_field, error and full_error helpers
|
183
|
+
(please take a look at the rdocs for each method for more info):
|
184
|
+
|
185
|
+
```erb
|
186
|
+
<%= simple_form_for @user do |f| %>
|
187
|
+
<%= f.label :username %>
|
188
|
+
<%= f.input_field :username %>
|
189
|
+
<%= f.hint 'No special characters, please!' %>
|
190
|
+
<%= f.error :username, :id => 'user_name_error' %>
|
191
|
+
<%= f.full_error :token %>
|
192
|
+
<%= f.submit 'Save' %>
|
193
|
+
<% end %>
|
194
|
+
```
|
195
|
+
|
196
|
+
Any extra option passed to these methods will be rendered as html option.
|
197
|
+
|
198
|
+
### Collections
|
199
|
+
|
200
|
+
And what if you want to create a select containing the age from 18 to 60 in your form? You can do it
|
201
|
+
overriding the `:collection` option:
|
202
|
+
|
203
|
+
```erb
|
204
|
+
<%= simple_form_for @user do |f| %>
|
205
|
+
<%= f.input :user %>
|
206
|
+
<%= f.input :age, :collection => 18..60 %>
|
207
|
+
<%= f.button :submit %>
|
208
|
+
<% end %>
|
209
|
+
```
|
210
|
+
|
211
|
+
Collections can be arrays or ranges, and when a `:collection` is given the `:select` input will be
|
212
|
+
rendered by default, so we don't need to pass the `:as => :select` option. Other types of collection
|
213
|
+
are `:radio_buttons` and `:check_boxes`. Those are added by **SimpleForm** to Rails set of form
|
214
|
+
helpers (read Extra Helpers session below for more information).
|
215
|
+
|
216
|
+
Collection inputs accept two other options beside collections:
|
217
|
+
|
218
|
+
* _label_method_ => the label method to be applied to the collection to retrieve the label (use this
|
219
|
+
instead of the `text_method` option in `collection_select`)
|
220
|
+
|
221
|
+
* _value_method_ => the value method to be applied to the collection to retrieve the value
|
222
|
+
|
223
|
+
Those methods are useful to manipulate the given collection. Both of these options also accept
|
224
|
+
lambda/procs in case you want to calculate the value or label in a special way eg. custom
|
225
|
+
translation. All other options given are sent straight to the underlying helper. For example, you
|
226
|
+
can give prompt as:
|
227
|
+
|
228
|
+
```ruby
|
229
|
+
f.input :age, :collection => 18..60, :prompt => "Select your age"
|
230
|
+
```
|
231
|
+
|
232
|
+
It is also possible to create grouped collection selects, that will use the html *optgroup* tags, like this:
|
233
|
+
|
234
|
+
```ruby
|
235
|
+
f.input :country_id, :collection => @continents, :as => :grouped_select, :group_method => :countries
|
236
|
+
```
|
237
|
+
|
238
|
+
Grouped collection inputs accept the same `:label_method` and `:value_method` options, which will be
|
239
|
+
used to retrieve label/value attributes for the `option` tags. Besides that, you can give:
|
240
|
+
|
241
|
+
* _group_method_ => the method to be called on the given collection to generate the options for
|
242
|
+
each group (required)
|
243
|
+
|
244
|
+
* _group_label_method_ => the label method to be applied on the given collection to retrieve the label
|
245
|
+
for the _optgroup_ (**SimpleForm** will attempt to guess the best one the same way it does with
|
246
|
+
`:label_method`)
|
247
|
+
|
248
|
+
### Priority
|
249
|
+
|
250
|
+
**SimpleForm** also supports `:time_zone` and `:country`. When using such helpers, you can give
|
251
|
+
`:priority` as option to select which time zones and/or countries should be given higher priority:
|
252
|
+
|
253
|
+
```ruby
|
254
|
+
f.input :residence_country, :priority => [ "Brazil" ]
|
255
|
+
f.input :time_zone, :priority => /US/
|
256
|
+
```
|
257
|
+
|
258
|
+
Those values can also be configured with a default value to be used site use through the
|
259
|
+
`SimpleForm.country_priority` and `SimpleForm.time_zone_priority` helpers.
|
260
|
+
|
261
|
+
Note: While using `country_select` if you want to restrict to only a subset of countries for a specific
|
262
|
+
drop down then you may use the `:collection` option:
|
263
|
+
|
264
|
+
```ruby
|
265
|
+
f.input :shipping_country, :priority => [ "Brazil" ], :collection => [ "Australia", "Brazil", "New Zealand"]
|
266
|
+
```
|
267
|
+
|
268
|
+
### Associations
|
269
|
+
|
270
|
+
To deal with associations, **SimpleForm** can generate select inputs, a series of radios buttons or check boxes.
|
271
|
+
Lets see how it works: imagine you have a user model that belongs to a company and has_and_belongs_to_many
|
272
|
+
roles. The structure would be something like:
|
273
|
+
|
274
|
+
```ruby
|
275
|
+
class User < ActiveRecord::Base
|
276
|
+
belongs_to :company
|
277
|
+
has_and_belongs_to_many :roles
|
278
|
+
end
|
279
|
+
|
280
|
+
class Company < ActiveRecord::Base
|
281
|
+
has_many :users
|
282
|
+
end
|
283
|
+
|
284
|
+
class Role < ActiveRecord::Base
|
285
|
+
has_and_belongs_to_many :users
|
286
|
+
end
|
287
|
+
```
|
288
|
+
|
289
|
+
Now we have the user form:
|
290
|
+
|
291
|
+
```erb
|
292
|
+
<%= simple_form_for @user do |f| %>
|
293
|
+
<%= f.input :name %>
|
294
|
+
<%= f.association :company %>
|
295
|
+
<%= f.association :roles %>
|
296
|
+
<%= f.button :submit %>
|
297
|
+
<% end %>
|
298
|
+
```
|
299
|
+
|
300
|
+
Simple enough, right? This is going to render a `:select` input for choosing the `:company`, and another
|
301
|
+
`:select` input with `:multiple` option for the `:roles`. You can, of course, change it to use radio
|
302
|
+
buttons and check boxes as well:
|
303
|
+
|
304
|
+
```ruby
|
305
|
+
f.association :company, :as => :radio_buttons
|
306
|
+
f.association :roles, :as => :check_boxes
|
307
|
+
```
|
308
|
+
|
309
|
+
The association helper just invokes `input` under the hood, so all options available to `:select`,
|
310
|
+
`:radio_buttons` and `:check_boxes` are also available to association. Additionally, you can specify
|
311
|
+
the collection by hand, all together with the prompt:
|
312
|
+
|
313
|
+
```ruby
|
314
|
+
f.association :company, :collection => Company.active.all(:order => 'name'), :prompt => "Choose a Company"
|
315
|
+
```
|
316
|
+
|
317
|
+
### Buttons
|
318
|
+
|
319
|
+
All web forms need buttons, right? **SimpleForm** wraps them in the DSL, acting like a proxy:
|
320
|
+
|
321
|
+
```erb
|
322
|
+
<%= simple_form_for @user do |f| %>
|
323
|
+
<%= f.input :name %>
|
324
|
+
<%= f.button :submit %>
|
325
|
+
<% end %>
|
326
|
+
```
|
327
|
+
|
328
|
+
The above will simply call submit. You choose to use it or not, it's just a question of taste.
|
329
|
+
|
330
|
+
### Wrapping Rails Form Helpers
|
331
|
+
|
332
|
+
Say you wanted to use a rails form helper but still wrap it in **SimpleForm** goodness? You can, by
|
333
|
+
calling input with a block like so:
|
334
|
+
|
335
|
+
```erb
|
336
|
+
<%= f.input :role do %>
|
337
|
+
<%= f.select :role, Role.all.map { |r| [r.name, r.id, { :class => r.company.id }] }, :include_blank => true %>
|
338
|
+
<% end %>
|
339
|
+
```
|
340
|
+
|
341
|
+
In the above example, we're taking advantage of Rails 3's select method that allows us to pass in a
|
342
|
+
hash of additional attributes for each option.
|
343
|
+
|
344
|
+
### Extra helpers
|
345
|
+
|
346
|
+
**SimpleForm** also comes with some extra helpers you can use inside rails default forms without relying
|
347
|
+
on `simple_form_for` helper. They are listed below.
|
348
|
+
|
349
|
+
#### Simple Fields For
|
350
|
+
|
351
|
+
Wrapper to use SimpleForm inside a default rails form
|
352
|
+
|
353
|
+
```ruby
|
354
|
+
form_for @user do |f|
|
355
|
+
f.simple_fields_for :posts do |posts_form|
|
356
|
+
# Here you have all simple_form methods available
|
357
|
+
posts_form.input :title
|
358
|
+
end
|
359
|
+
end
|
360
|
+
```
|
361
|
+
|
362
|
+
#### Collection Radio Buttons
|
363
|
+
|
364
|
+
Creates a collection of radio inputs with labels associated (same API as `collection_select`):
|
365
|
+
|
366
|
+
```ruby
|
367
|
+
form_for @user do |f|
|
368
|
+
f.collection_radio_buttons :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
|
369
|
+
end
|
370
|
+
```
|
371
|
+
|
372
|
+
```html
|
373
|
+
<input id="user_options_true" name="user[options]" type="radio" value="true" />
|
374
|
+
<label class="collection_radio_buttons" for="user_options_true">Yes</label>
|
375
|
+
<input id="user_options_false" name="user[options]" type="radio" value="false" />
|
376
|
+
<label class="collection_radio_buttons" for="user_options_false">No</label>
|
377
|
+
```
|
378
|
+
|
379
|
+
#### Collection Check Boxes
|
380
|
+
|
381
|
+
Creates a collection of check boxes with labels associated (same API as `collection_select`):
|
382
|
+
|
383
|
+
```ruby
|
384
|
+
form_for @user do |f|
|
385
|
+
f.collection_check_boxes :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
|
386
|
+
end
|
387
|
+
```
|
388
|
+
|
389
|
+
```html
|
390
|
+
<input name="user[options][]" type="hidden" value="" />
|
391
|
+
<input id="user_options_true" name="user[options][]" type="checkbox" value="true" />
|
392
|
+
<label class="collection_check_box" for="user_options_true">Yes</label>
|
393
|
+
<input name="user[options][]" type="hidden" value="" />
|
394
|
+
<input id="user_options_false" name="user[options][]" type="checkbox" value="false" />
|
395
|
+
<label class="collection_check_box" for="user_options_false">No</label>
|
396
|
+
```
|
397
|
+
|
398
|
+
To use this with associations in your model, you can do the following:
|
399
|
+
|
400
|
+
```ruby
|
401
|
+
form_for @user do |f|
|
402
|
+
f.collection_check_boxes :role_ids, Role.all, :id, :name # using :roles here is not going to work.
|
403
|
+
end
|
404
|
+
```
|
405
|
+
|
406
|
+
## Mappings/Inputs available
|
407
|
+
|
408
|
+
**SimpleForm** comes with a lot of default mappings:
|
409
|
+
|
410
|
+
```text
|
411
|
+
Mapping Input Column Type
|
412
|
+
|
413
|
+
boolean check box boolean
|
414
|
+
string text field string
|
415
|
+
email email field string with name matching "email"
|
416
|
+
url url field string with name matching "url"
|
417
|
+
tel tel field string with name matching "phone"
|
418
|
+
password password field string with name matching "password"
|
419
|
+
search search -
|
420
|
+
text text area text
|
421
|
+
file file field string, responding to file methods
|
422
|
+
hidden hidden field -
|
423
|
+
integer number field integer
|
424
|
+
float number field float
|
425
|
+
decimal number field decimal
|
426
|
+
range range field -
|
427
|
+
datetime datetime select datetime/timestamp
|
428
|
+
date date select date
|
429
|
+
time time select time
|
430
|
+
select collection select belongs_to/has_many/has_and_belongs_to_many associations
|
431
|
+
radio_buttons collection radio buttons belongs_to
|
432
|
+
check_boxes collection check boxes has_many/has_and_belongs_to_many associations
|
433
|
+
country country select string with name matching "country"
|
434
|
+
time_zone time zone select string with name matching "time_zone"
|
435
|
+
```
|
436
|
+
|
437
|
+
## Custom inputs
|
438
|
+
|
439
|
+
It is very easy to add custom inputs to **SimpleForm**. For instance, if you want to add a custom input
|
440
|
+
that extends the string one, you just need to add this file:
|
441
|
+
|
442
|
+
```ruby
|
443
|
+
# app/inputs/currency_input.rb
|
444
|
+
class CurrencyInput < SimpleForm::Inputs::Base
|
445
|
+
def input
|
446
|
+
"$ #{@builder.text_field(attribute_name, input_html_options)}".html_safe
|
447
|
+
end
|
448
|
+
end
|
449
|
+
```
|
450
|
+
|
451
|
+
And use it in your views:
|
452
|
+
|
453
|
+
```ruby
|
454
|
+
f.input :money, :as => :currency
|
455
|
+
```
|
456
|
+
|
457
|
+
You can also redefine existing **SimpleForm** inputs by creating a new class with the same name. For
|
458
|
+
instance, if you want to wrap date/time/datetime in a div, you can do:
|
459
|
+
|
460
|
+
```ruby
|
461
|
+
# app/inputs/date_time_input.rb
|
462
|
+
class DateTimeInput < SimpleForm::Inputs::DateTimeInput
|
463
|
+
def input
|
464
|
+
"<div>#{super}</div>".html_safe
|
465
|
+
end
|
466
|
+
end
|
467
|
+
```
|
468
|
+
|
469
|
+
## Custom form builder
|
470
|
+
|
471
|
+
You can create a custom form builder that uses **SimpleForm**.
|
472
|
+
|
473
|
+
Create a helper method that calls `simple_form_for` with a custom builder:
|
474
|
+
|
475
|
+
```ruby
|
476
|
+
def custom_form_for(object, *args, &block)
|
477
|
+
options = args.extract_options!
|
478
|
+
simple_form_for(object, *(args << options.merge(:builder => CustomFormBuilder)), &block)
|
479
|
+
end
|
480
|
+
```
|
481
|
+
|
482
|
+
Create a form builder class that inherits from `SimpleForm::FormBuilder`.
|
483
|
+
|
484
|
+
```ruby
|
485
|
+
class CustomFormBuilder < SimpleForm::FormBuilder
|
486
|
+
def input(attribute_name, options = {}, &block)
|
487
|
+
options[:input_html].merge! :class => 'custom'
|
488
|
+
super
|
489
|
+
end
|
490
|
+
end
|
491
|
+
```
|
492
|
+
|
493
|
+
## I18n
|
494
|
+
|
495
|
+
**SimpleForm** uses all power of I18n API to lookup labels, hints and placeholders. To customize your
|
496
|
+
forms you can create a locale file like this:
|
497
|
+
|
498
|
+
```yaml
|
499
|
+
en:
|
500
|
+
simple_form:
|
501
|
+
labels:
|
502
|
+
user:
|
503
|
+
username: 'User name'
|
504
|
+
password: 'Password'
|
505
|
+
hints:
|
506
|
+
user:
|
507
|
+
username: 'User name to sign in.'
|
508
|
+
password: 'No special characters, please.'
|
509
|
+
placeholders:
|
510
|
+
user:
|
511
|
+
username: 'Your username'
|
512
|
+
password: '****'
|
513
|
+
```
|
514
|
+
|
515
|
+
And your forms will use this information to render the components for you.
|
516
|
+
|
517
|
+
**SimpleForm** also lets you be more specific, separating lookups through actions for labels, hints and
|
518
|
+
placeholders. Let's say you want a different label for new and edit actions, the locale file would
|
519
|
+
be something like:
|
520
|
+
|
521
|
+
```yaml
|
522
|
+
en:
|
523
|
+
simple_form:
|
524
|
+
labels:
|
525
|
+
user:
|
526
|
+
username: 'User name'
|
527
|
+
password: 'Password'
|
528
|
+
edit:
|
529
|
+
username: 'Change user name'
|
530
|
+
password: 'Change password'
|
531
|
+
```
|
532
|
+
|
533
|
+
This way **SimpleForm** will figure out the right translation for you, based on the action being
|
534
|
+
rendered. And to be a little bit DRYer with your locale file, you can specify defaults for all
|
535
|
+
models under the 'defaults' key:
|
536
|
+
|
537
|
+
```yaml
|
538
|
+
en:
|
539
|
+
simple_form:
|
540
|
+
labels:
|
541
|
+
defaults:
|
542
|
+
username: 'User name'
|
543
|
+
password: 'Password'
|
544
|
+
new:
|
545
|
+
username: 'Choose a user name'
|
546
|
+
hints:
|
547
|
+
defaults:
|
548
|
+
username: 'User name to sign in.'
|
549
|
+
password: 'No special characters, please.'
|
550
|
+
placeholders:
|
551
|
+
defaults:
|
552
|
+
username: 'Your username'
|
553
|
+
password: '****'
|
554
|
+
```
|
555
|
+
|
556
|
+
**SimpleForm** will always look for a default attribute translation under the "defaults" key if no
|
557
|
+
specific is found inside the model key.Note that this syntax is different from 1.x. To migrate to
|
558
|
+
the new syntax, just move "labels.#{attribute}" to "labels.defaults.#{attribute}".
|
559
|
+
|
560
|
+
In addition, **SimpleForm** will fallback to default human_attribute_name from Rails when no other
|
561
|
+
translation is found for labels. Finally, you can also overwrite any label, hint or placeholder
|
562
|
+
inside your view, just by passing the option manually. This way the I18n lookup will be skipped.
|
563
|
+
|
564
|
+
**SimpleForm** also has support for translating options in collection helpers. For instance, given a
|
565
|
+
User with a `:gender` attribute, you might want to create a select box showing translated labels
|
566
|
+
that would post either `male` or `female` as value. With **SimpleForm** you could create an input
|
567
|
+
like this:
|
568
|
+
|
569
|
+
```ruby
|
570
|
+
f.input :gender, :collection => [:male, :female]
|
571
|
+
```
|
572
|
+
|
573
|
+
And **SimpleForm** will try a lookup like this in your locale file, to find the right labels to show:
|
574
|
+
|
575
|
+
```yaml
|
576
|
+
en:
|
577
|
+
simple_form:
|
578
|
+
options:
|
579
|
+
user:
|
580
|
+
gender:
|
581
|
+
male: 'Male'
|
582
|
+
female: "Female'
|
583
|
+
```
|
584
|
+
|
585
|
+
You can also use the `defaults` key as you would do with labels, hints and placeholders. It is
|
586
|
+
important to notice that **SimpleForm** will only do the lookup for options if you give a collection
|
587
|
+
composed of symbols only. This is to avoid constant lookups to I18n.
|
588
|
+
|
589
|
+
It's also possible to translate buttons, using Rails' built-in I18n support:
|
590
|
+
|
591
|
+
```yaml
|
592
|
+
en:
|
593
|
+
helpers:
|
594
|
+
submit:
|
595
|
+
user:
|
596
|
+
create: "Add %{model}"
|
597
|
+
update: "Save Changes"
|
598
|
+
```
|
599
|
+
|
600
|
+
There are other options that can be configured through I18n API, such as required text and boolean.
|
601
|
+
Be sure to check our locale file or the one copied to your application after you run
|
602
|
+
`rails generate simple_form:install`.
|
603
|
+
|
604
|
+
## Configuration
|
605
|
+
|
606
|
+
**SimpleForm** has several configuration options. You can read and change them in the initializer
|
607
|
+
created by **SimpleForm**, so if you haven't executed the command below yet, please do:
|
608
|
+
|
609
|
+
`rails generate simple_form:install`
|
610
|
+
|
611
|
+
### The wrappers API
|
612
|
+
|
613
|
+
With **SimpleForm** you can configure how your components will be rendered using the wrappers API.
|
614
|
+
The syntax looks like this:
|
615
|
+
|
616
|
+
```ruby
|
617
|
+
config.wrappers :tag => :div, :class => :input,
|
618
|
+
:error_class => :field_with_errors do |b|
|
619
|
+
|
620
|
+
# Form extensions
|
621
|
+
b.use :html5
|
622
|
+
b.optional :pattern
|
623
|
+
b.use :maxlength
|
624
|
+
b.use :placeholder
|
625
|
+
b.use :readonly
|
626
|
+
|
627
|
+
# Form components
|
628
|
+
b.use :label_input
|
629
|
+
b.use :hint, :wrap_with => { :tag => :span, :class => :hint }
|
630
|
+
b.use :error, :wrap_with => { :tag => :span, :class => :error }
|
631
|
+
end
|
632
|
+
```
|
633
|
+
|
634
|
+
The _Form components_ will generate the form tags like labels, inputs, hints or errors contents.
|
635
|
+
|
636
|
+
The _Form extensions_ are used to generate some attributes or perform some lookups on the model to
|
637
|
+
add extra information to your components.
|
638
|
+
|
639
|
+
You can create new _Form components_ using the wrappers API as in the following example:
|
640
|
+
|
641
|
+
```ruby
|
642
|
+
config.wrappers do |b|
|
643
|
+
b.use :placeholder
|
644
|
+
b.use :label_input
|
645
|
+
b.wrapper :tag => :div, :class => 'separator' do |component|
|
646
|
+
component.use :hint, :wrap_with => { :tag => :span, :class => :hint }
|
647
|
+
component.use :error, :wrap_with => { :tag => :span, :class => :error }
|
648
|
+
end
|
649
|
+
end
|
650
|
+
```
|
651
|
+
|
652
|
+
this will wrap the hint and error components within a `div` tag using the class `'separator'`.
|
653
|
+
|
654
|
+
If you want to customize the custom _Form components_ on demand you can give it a name like this:
|
655
|
+
|
656
|
+
```ruby
|
657
|
+
config.wrappers do |b|
|
658
|
+
b.use :placeholder
|
659
|
+
b.use :label_input
|
660
|
+
b.wrapper :my_wrapper, :tag => :div, :class => 'separator' do |component|
|
661
|
+
component.use :hint, :wrap_with => { :tag => :span, :class => :hint }
|
662
|
+
component.use :error, :wrap_with => { :tag => :span, :class => :error }
|
663
|
+
end
|
664
|
+
end
|
665
|
+
```
|
666
|
+
|
667
|
+
and now you can pass options to your `input` calls to customize the `:my_wrapper` _Form component_.
|
668
|
+
|
669
|
+
```ruby
|
670
|
+
# Completely turns off the custom wrapper
|
671
|
+
f.input :name, :my_wrapper => false
|
672
|
+
|
673
|
+
# Configure the html
|
674
|
+
f.input :name, :my_wrapper_html => { :id => 'special_id' }
|
675
|
+
|
676
|
+
# Configure the tag
|
677
|
+
f.input :name, :my_wrapper_tag => :p
|
678
|
+
```
|
679
|
+
|
680
|
+
You can also define more than one wrapper and pick one to render in a specific form or input.
|
681
|
+
To define another wrapper you have to give it a name, as the follow:
|
682
|
+
|
683
|
+
```ruby
|
684
|
+
config.wrappers :small do |b|
|
685
|
+
b.use :placeholder
|
686
|
+
b.use :label_input
|
687
|
+
end
|
688
|
+
```
|
689
|
+
|
690
|
+
and use it in this way:
|
691
|
+
|
692
|
+
```ruby
|
693
|
+
# Specifying to whole form
|
694
|
+
simple_form_for @user, :wrapper => :small do |f|
|
695
|
+
f.input :name
|
696
|
+
end
|
697
|
+
|
698
|
+
# Specifying to one input
|
699
|
+
simple_form_for @user do |f|
|
700
|
+
f.input :name, :wrapper => :small
|
701
|
+
end
|
702
|
+
```
|
703
|
+
|
704
|
+
**SimpleForm** also allows you to use optional elements. For instance, let's suppose you want to use
|
705
|
+
hints or placeholders, but you don't want them to be generated automatically. You can set their
|
706
|
+
default values to `false` or use the `optional` method. Is preferible to use the `optional` syntax:
|
707
|
+
|
708
|
+
```ruby
|
709
|
+
config.wrappers :placeholder => false do |b|
|
710
|
+
b.use :placeholder
|
711
|
+
b.use :label_input
|
712
|
+
b.wrapper :tag => :div, :class => 'separator' do |component|
|
713
|
+
component.optional :hint, :wrap_with => { :tag => :span, :class => :hint }
|
714
|
+
component.use :error, :wrap_with => { :tag => :span, :class => :error }
|
715
|
+
end
|
716
|
+
end
|
717
|
+
```
|
718
|
+
|
719
|
+
By setting it as `optional`, a hint will only be generated when `:hint => true` is explicitly used.
|
720
|
+
The same for placehold.
|
721
|
+
|
722
|
+
## HTML 5 Notice
|
723
|
+
|
724
|
+
By default, **SimpleForm** will generate input field types and attributes that are supported in HTML5,
|
725
|
+
but are considered invalid HTML for older document types such as HTML4 or XHTML1.0. The HTML5
|
726
|
+
extensions include the new field types such as email, number, search, url, tel, and the new
|
727
|
+
attributes such as required, autofocus, maxlength, min, max, step.
|
728
|
+
|
729
|
+
Most browsers will not care, but some of the newer ones - in particular Chrome 10+ - use the
|
730
|
+
required attribute to force a value into an input and will prevent form submission without it.
|
731
|
+
Depending on the design of the application this may or may not be desired. In many cases it can
|
732
|
+
break existing UI's.
|
733
|
+
|
734
|
+
It is possible to disable all HTML 5 extensions in **SimpleForm** with the following configuration:
|
735
|
+
|
736
|
+
```ruby
|
737
|
+
SimpleForm.html5 = false # default is true
|
738
|
+
```
|
739
|
+
|
740
|
+
If you want to have all other HTML 5 features, such as the new field types, you can disable only
|
741
|
+
the browser validation:
|
742
|
+
|
743
|
+
```ruby
|
744
|
+
SimpleForm.browser_validations = false # default is true
|
745
|
+
```
|
746
|
+
|
747
|
+
This option adds a new `novalidate` property to the form, instructing it to skip all HTML 5
|
748
|
+
validation. The inputs will still be generated with the required and other attributes, that might
|
749
|
+
help you to use some generic javascript validation.
|
750
|
+
|
751
|
+
You can also add `novalidate` to a specific form by setting the option on the form itself:
|
752
|
+
|
753
|
+
```erb
|
754
|
+
<%= simple_form_for(resource, :html => {:novalidate => true}) do |form| %>
|
755
|
+
```
|
756
|
+
|
757
|
+
Please notice that any of the configurations above will disable the `placeholder` component,
|
758
|
+
which is an HTML 5 feature. We believe most of the newest browsers are handling this attribute fine,
|
759
|
+
and if they aren't, any plugin you use would take of using the placeholder attribute to do it.
|
760
|
+
However, you can disable it if you want, by removing the placeholder component from the components
|
761
|
+
list in **SimpleForm** configuration file.
|
762
|
+
|
763
|
+
## Information
|
764
|
+
|
765
|
+
### Google Group
|
766
|
+
|
767
|
+
If you have any questions, comments, or concerns please use the Google Group instead of the GitHub
|
768
|
+
Issues tracker:
|
769
|
+
|
770
|
+
http://groups.google.com/group/plataformatec-simpleform
|
771
|
+
|
772
|
+
### RDocs
|
773
|
+
|
774
|
+
You can view the **SimpleForm** documentation in RDoc format here:
|
775
|
+
|
776
|
+
http://rubydoc.info/github/plataformatec/simple_form/master/frames
|
777
|
+
|
778
|
+
If you need to use **SimpleForm** with Rails 2.3, you can always run `gem server` from the command line
|
779
|
+
after you install the gem to access the old documentation.
|
780
|
+
|
781
|
+
### Bug reports
|
782
|
+
|
783
|
+
If you discover any bugs, feel free to create an issue on GitHub. Please add as much information as
|
784
|
+
possible to help us fixing the possible bug. We also encourage you to help even more by forking and
|
785
|
+
sending us a pull request.
|
786
|
+
|
787
|
+
https://github.com/plataformatec/simple_form/issues
|
788
|
+
|
789
|
+
## Maintainers
|
790
|
+
|
791
|
+
* José Valim (https://github.com/josevalim)
|
792
|
+
* Carlos Antonio da Silva (https://github.com/carlosantoniodasilva)
|
793
|
+
* Rafael Mendonça França (https://github.com/rafaelfranca)
|
794
|
+
|
795
|
+
## License
|
796
|
+
|
797
|
+
MIT License. Copyright 2012 Plataforma Tecnologia. http://blog.plataformatec.com.br
|