simple_form 1.5.2 → 2.0.0

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