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