mini-sharp-mod 0.0.1

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 (70) hide show
  1. checksums.yaml +7 -0
  2. data/mini-sharp-mod.gemspec +12 -0
  3. data/simple_form-5.4.1/CHANGELOG.md +259 -0
  4. data/simple_form-5.4.1/MIT-LICENSE +21 -0
  5. data/simple_form-5.4.1/README.md +1334 -0
  6. data/simple_form-5.4.1/lib/generators/simple_form/USAGE +3 -0
  7. data/simple_form-5.4.1/lib/generators/simple_form/install_generator.rb +42 -0
  8. data/simple_form-5.4.1/lib/generators/simple_form/templates/README +10 -0
  9. data/simple_form-5.4.1/lib/generators/simple_form/templates/_form.html.erb +15 -0
  10. data/simple_form-5.4.1/lib/generators/simple_form/templates/_form.html.haml +12 -0
  11. data/simple_form-5.4.1/lib/generators/simple_form/templates/_form.html.slim +11 -0
  12. data/simple_form-5.4.1/lib/generators/simple_form/templates/config/initializers/simple_form.rb +176 -0
  13. data/simple_form-5.4.1/lib/generators/simple_form/templates/config/initializers/simple_form_bootstrap.rb +372 -0
  14. data/simple_form-5.4.1/lib/generators/simple_form/templates/config/initializers/simple_form_foundation.rb +122 -0
  15. data/simple_form-5.4.1/lib/generators/simple_form/templates/config/locales/simple_form.en.yml +31 -0
  16. data/simple_form-5.4.1/lib/simple_form/action_view_extensions/builder.rb +38 -0
  17. data/simple_form-5.4.1/lib/simple_form/action_view_extensions/form_helper.rb +72 -0
  18. data/simple_form-5.4.1/lib/simple_form/components/errors.rb +74 -0
  19. data/simple_form-5.4.1/lib/simple_form/components/hints.rb +24 -0
  20. data/simple_form-5.4.1/lib/simple_form/components/html5.rb +33 -0
  21. data/simple_form-5.4.1/lib/simple_form/components/label_input.rb +34 -0
  22. data/simple_form-5.4.1/lib/simple_form/components/labels.rb +88 -0
  23. data/simple_form-5.4.1/lib/simple_form/components/maxlength.rb +35 -0
  24. data/simple_form-5.4.1/lib/simple_form/components/min_max.rb +41 -0
  25. data/simple_form-5.4.1/lib/simple_form/components/minlength.rb +35 -0
  26. data/simple_form-5.4.1/lib/simple_form/components/pattern.rb +27 -0
  27. data/simple_form-5.4.1/lib/simple_form/components/placeholders.rb +17 -0
  28. data/simple_form-5.4.1/lib/simple_form/components/readonly.rb +23 -0
  29. data/simple_form-5.4.1/lib/simple_form/components.rb +24 -0
  30. data/simple_form-5.4.1/lib/simple_form/error_notification.rb +49 -0
  31. data/simple_form-5.4.1/lib/simple_form/form_builder.rb +723 -0
  32. data/simple_form-5.4.1/lib/simple_form/helpers/autofocus.rb +12 -0
  33. data/simple_form-5.4.1/lib/simple_form/helpers/disabled.rb +16 -0
  34. data/simple_form-5.4.1/lib/simple_form/helpers/readonly.rb +16 -0
  35. data/simple_form-5.4.1/lib/simple_form/helpers/required.rb +36 -0
  36. data/simple_form-5.4.1/lib/simple_form/helpers/validators.rb +66 -0
  37. data/simple_form-5.4.1/lib/simple_form/helpers.rb +13 -0
  38. data/simple_form-5.4.1/lib/simple_form/inputs/base.rb +232 -0
  39. data/simple_form-5.4.1/lib/simple_form/inputs/block_input.rb +15 -0
  40. data/simple_form-5.4.1/lib/simple_form/inputs/boolean_input.rb +108 -0
  41. data/simple_form-5.4.1/lib/simple_form/inputs/collection_check_boxes_input.rb +22 -0
  42. data/simple_form-5.4.1/lib/simple_form/inputs/collection_input.rb +122 -0
  43. data/simple_form-5.4.1/lib/simple_form/inputs/collection_radio_buttons_input.rb +59 -0
  44. data/simple_form-5.4.1/lib/simple_form/inputs/collection_select_input.rb +17 -0
  45. data/simple_form-5.4.1/lib/simple_form/inputs/color_input.rb +14 -0
  46. data/simple_form-5.4.1/lib/simple_form/inputs/date_time_input.rb +39 -0
  47. data/simple_form-5.4.1/lib/simple_form/inputs/file_input.rb +12 -0
  48. data/simple_form-5.4.1/lib/simple_form/inputs/grouped_collection_select_input.rb +54 -0
  49. data/simple_form-5.4.1/lib/simple_form/inputs/hidden_input.rb +20 -0
  50. data/simple_form-5.4.1/lib/simple_form/inputs/numeric_input.rb +20 -0
  51. data/simple_form-5.4.1/lib/simple_form/inputs/password_input.rb +14 -0
  52. data/simple_form-5.4.1/lib/simple_form/inputs/priority_input.rb +37 -0
  53. data/simple_form-5.4.1/lib/simple_form/inputs/range_input.rb +15 -0
  54. data/simple_form-5.4.1/lib/simple_form/inputs/rich_text_area_input.rb +14 -0
  55. data/simple_form-5.4.1/lib/simple_form/inputs/string_input.rb +25 -0
  56. data/simple_form-5.4.1/lib/simple_form/inputs/text_input.rb +14 -0
  57. data/simple_form-5.4.1/lib/simple_form/inputs/weekday_input.rb +14 -0
  58. data/simple_form-5.4.1/lib/simple_form/inputs.rb +27 -0
  59. data/simple_form-5.4.1/lib/simple_form/map_type.rb +17 -0
  60. data/simple_form-5.4.1/lib/simple_form/railtie.rb +19 -0
  61. data/simple_form-5.4.1/lib/simple_form/tags.rb +73 -0
  62. data/simple_form-5.4.1/lib/simple_form/version.rb +4 -0
  63. data/simple_form-5.4.1/lib/simple_form/wrappers/builder.rb +80 -0
  64. data/simple_form-5.4.1/lib/simple_form/wrappers/leaf.rb +29 -0
  65. data/simple_form-5.4.1/lib/simple_form/wrappers/many.rb +74 -0
  66. data/simple_form-5.4.1/lib/simple_form/wrappers/root.rb +43 -0
  67. data/simple_form-5.4.1/lib/simple_form/wrappers/single.rb +27 -0
  68. data/simple_form-5.4.1/lib/simple_form/wrappers.rb +10 -0
  69. data/simple_form-5.4.1/lib/simple_form.rb +340 -0
  70. metadata +109 -0
@@ -0,0 +1,1334 @@
1
+ ![Simple Form Logo](https://raw.github.com/heartcombo/simple_form/main/simple_form.png)
2
+
3
+ [![Gem Version](https://fury-badge.herokuapp.com/rb/simple_form.png)](http://badge.fury.io/rb/simple_form)
4
+
5
+ Rails forms made easy.
6
+
7
+ **Simple Form** aims to be as flexible as possible while helping you with powerful components to create
8
+ your forms. The basic goal of **Simple Form** is to not touch your way of defining the layout, letting
9
+ you find the better design for your eyes. Most of the DSL was inherited from Formtastic,
10
+ which we are thankful for and should make you feel right at home.
11
+
12
+ INFO: This README refers to **Simple Form** 5.0. For older releases, check the related branch for your version.
13
+
14
+ <!-- START doctoc generated TOC please keep comment here to allow auto update -->
15
+ <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
16
+ ## Table of Contents
17
+
18
+ - [Installation](#installation)
19
+ - [Bootstrap](#bootstrap-5)
20
+ - [Zurb Foundation 5](#zurb-foundation-5)
21
+ - [Country Select](#country-select)
22
+ - [Usage](#usage)
23
+ - [Stripping away all wrapper divs](#stripping-away-all-wrapper-divs)
24
+ - [Collections](#collections)
25
+ - [Priority](#priority)
26
+ - [Associations](#associations)
27
+ - [Buttons](#buttons)
28
+ - [Wrapping Rails Form Helpers](#wrapping-rails-form-helpers)
29
+ - [Extra helpers](#extra-helpers)
30
+ - [Simple Fields For](#simple-fields-for)
31
+ - [Collection Radio Buttons](#collection-radio-buttons)
32
+ - [Collection Check Boxes](#collection-check-boxes)
33
+ - [Available input types and defaults for each column type](#available-input-types-and-defaults-for-each-column-type)
34
+ - [Custom inputs](#custom-inputs)
35
+ - [Custom form builder](#custom-form-builder)
36
+ - [I18n](#i18n)
37
+ - [Configuration](#configuration)
38
+ - [The wrappers API](#the-wrappers-api)
39
+ - [Custom Components](#custom-components)
40
+ - [HTML 5 Notice](#html-5-notice)
41
+ - [Using non Active Record objects](#using-non-active-record-objects)
42
+ - [Information](#information)
43
+ - [RDocs](#rdocs)
44
+ - [Supported Ruby / Rails versions](#supported-ruby--rails-versions)
45
+ - [Bug reports](#bug-reports)
46
+ - [Maintainers](#maintainers)
47
+ - [License](#license)
48
+
49
+ <!-- END doctoc generated TOC please keep comment here to allow auto update -->
50
+
51
+ ## Installation
52
+
53
+ Add it to your Gemfile:
54
+
55
+ ```ruby
56
+ gem 'simple_form'
57
+ ```
58
+
59
+ Run the following command to install it:
60
+
61
+ ```console
62
+ bundle install
63
+ ```
64
+
65
+ Run the generator:
66
+
67
+ ```console
68
+ rails generate simple_form:install
69
+ ```
70
+
71
+ ### Bootstrap 5
72
+
73
+ **Simple Form** can be easily integrated with [Bootstrap 5](http://getbootstrap.com/).
74
+ Use the `bootstrap` option in the install generator, like this:
75
+
76
+ ```console
77
+ rails generate simple_form:install --bootstrap
78
+ ```
79
+
80
+ This will add an initializer that configures **Simple Form** wrappers for
81
+ Bootstrap 5's [form controls](https://getbootstrap.com/docs/5.0/forms/overview/).
82
+ You have to be sure that you added a copy of the [Bootstrap](http://getbootstrap.com/)
83
+ assets on your application.
84
+
85
+ For more information see the generator output, our
86
+ [example application code](https://github.com/heartcombo/simple_form-bootstrap) and
87
+ [the live example app](https://simple-form-bootstrap.herokuapp.com/).
88
+
89
+ ### Zurb Foundation 5
90
+
91
+ To generate wrappers that are compatible with [Zurb Foundation 5](http://foundation.zurb.com/), pass
92
+ the `foundation` option to the generator, like this:
93
+
94
+ ```console
95
+ rails generate simple_form:install --foundation
96
+ ```
97
+
98
+ Please note that the Foundation wrapper does not support the `:hint` option by default. In order to
99
+ enable hints, please uncomment the appropriate line in `config/initializers/simple_form_foundation.rb`.
100
+ You will need to provide your own CSS styles for hints.
101
+
102
+ Please see the [instructions on how to install Foundation in a Rails app](http://foundation.zurb.com/docs/applications.html).
103
+
104
+ ### Country Select
105
+
106
+ If you want to use the country select, you will need the
107
+ [country_select gem](https://rubygems.org/gems/country_select), add it to your Gemfile:
108
+
109
+ ```ruby
110
+ gem 'country_select'
111
+ ```
112
+
113
+ If you don't want to use the gem you can easily override this behaviour by mapping the
114
+ country inputs to something else, with a line like this in your `simple_form.rb` initializer:
115
+
116
+ ```ruby
117
+ config.input_mappings = { /country/ => :string }
118
+ ```
119
+
120
+ ## Usage
121
+
122
+ **Simple Form** was designed to be customized as you need to. Basically it's a stack of components that
123
+ are invoked to create a complete html input for you, which by default contains label, hints, errors
124
+ and the input itself. It does not aim to create a lot of different logic from the default Rails
125
+ form helpers, as they do a great job by themselves. Instead, **Simple Form** acts as a DSL and just
126
+ maps your input type (retrieved from the column definition in the database) to a specific helper method.
127
+
128
+ To start using **Simple Form** you just have to use the helper it provides:
129
+
130
+ ```erb
131
+ <%= simple_form_for @user do |f| %>
132
+ <%= f.input :username %>
133
+ <%= f.input :password %>
134
+ <%= f.button :submit %>
135
+ <% end %>
136
+ ```
137
+
138
+ This will generate an entire form with labels for user name and password as well, and render errors
139
+ by default when you render the form with invalid data (after submitting for example).
140
+
141
+ You can overwrite the default label by passing it to the input method. You can also add a hint,
142
+ an error, or even a placeholder. For boolean inputs, you can add an inline label as well:
143
+
144
+ ```erb
145
+ <%= simple_form_for @user do |f| %>
146
+ <%= f.input :username, label: 'Your username please', error: 'Username is mandatory, please specify one' %>
147
+ <%= f.input :password, hint: 'No special characters.' %>
148
+ <%= f.input :email, placeholder: 'user@domain.com' %>
149
+ <%= f.input :remember_me, inline_label: 'Yes, remember me' %>
150
+ <%= f.button :submit %>
151
+ <% end %>
152
+ ```
153
+
154
+ In some cases you may want to disable labels, hints or errors. Or you may want to configure the html
155
+ of any of them:
156
+
157
+ ```erb
158
+ <%= simple_form_for @user do |f| %>
159
+ <%= f.input :username, label_html: { class: 'my_class' }, hint_html: { class: 'hint_class' } %>
160
+ <%= f.input :password, hint: false, error_html: { id: 'password_error' } %>
161
+ <%= f.input :password_confirmation, label: false %>
162
+ <%= f.button :submit %>
163
+ <% end %>
164
+ ```
165
+
166
+ It is also possible to pass any html attribute straight to the input, by using the `:input_html`
167
+ option, for instance:
168
+
169
+ ```erb
170
+ <%= simple_form_for @user do |f| %>
171
+ <%= f.input :username, input_html: { class: 'special' } %>
172
+ <%= f.input :password, input_html: { maxlength: 20 } %>
173
+ <%= f.input :remember_me, input_html: { value: '1' } %>
174
+ <%= f.button :submit %>
175
+ <% end %>
176
+ ```
177
+
178
+ If you want to pass the same options to all inputs in the form (for example, a default class),
179
+ you can use the `:defaults` option in `simple_form_for`. Specific options in `input` call will
180
+ overwrite the defaults:
181
+
182
+ ```erb
183
+ <%= simple_form_for @user, defaults: { input_html: { class: 'default_class' } } do |f| %>
184
+ <%= f.input :username, input_html: { class: 'special' } %>
185
+ <%= f.input :password, input_html: { maxlength: 20 } %>
186
+ <%= f.input :remember_me, input_html: { value: '1' } %>
187
+ <%= f.button :submit %>
188
+ <% end %>
189
+ ```
190
+
191
+ Since **Simple Form** generates a wrapper div around your label and input by default, you can pass
192
+ any html attribute to that wrapper as well using the `:wrapper_html` option, like so:
193
+
194
+ ```erb
195
+ <%= simple_form_for @user do |f| %>
196
+ <%= f.input :username, wrapper_html: { class: 'username' } %>
197
+ <%= f.input :password, wrapper_html: { id: 'password' } %>
198
+ <%= f.input :remember_me, wrapper_html: { class: 'options' } %>
199
+ <%= f.button :submit %>
200
+ <% end %>
201
+ ```
202
+
203
+ Required fields are marked with an * prepended to their labels.
204
+
205
+ By default all inputs are required. When the form object includes `ActiveModel::Validations`
206
+ (which, for example, happens with Active Record models), fields are required only when there is `presence` validation.
207
+ Otherwise, **Simple Form** will mark fields as optional. For performance reasons, this
208
+ detection is skipped on validations that make use of conditional options, such as `:if` and `:unless`.
209
+
210
+ And of course, the `required` property of any input can be overwritten as needed:
211
+
212
+ ```erb
213
+ <%= simple_form_for @user do |f| %>
214
+ <%= f.input :name, required: false %>
215
+ <%= f.input :username %>
216
+ <%= f.input :password %>
217
+ <%= f.button :submit %>
218
+ <% end %>
219
+ ```
220
+
221
+ By default, **Simple Form** will look at the column type in the database and use an
222
+ appropriate input for the column. For example, a column created with type
223
+ `:text` in the database will use a `textarea` input by default. See the section
224
+ [Available input types and defaults for each column
225
+ type](https://github.com/heartcombo/simple_form#available-input-types-and-defaults-for-each-column-type)
226
+ for a complete list of defaults.
227
+
228
+ **Simple Form** also lets you overwrite the default input type it creates:
229
+
230
+ ```erb
231
+ <%= simple_form_for @user do |f| %>
232
+ <%= f.input :username %>
233
+ <%= f.input :password %>
234
+ <%= f.input :description, as: :text %>
235
+ <%= f.input :accepts, as: :radio_buttons %>
236
+ <%= f.button :submit %>
237
+ <% end %>
238
+ ```
239
+
240
+ So instead of a checkbox for the *accepts* attribute, you'll have a pair of radio buttons with yes/no
241
+ labels and a textarea instead of a text field for the description. You can also render boolean
242
+ attributes using `as: :select` to show a dropdown.
243
+
244
+ It is also possible to give the `:disabled` option to **Simple Form**, and it'll automatically mark
245
+ the wrapper as disabled with a CSS class, so you can style labels, hints and other components inside
246
+ the wrapper as well:
247
+
248
+ ```erb
249
+ <%= simple_form_for @user do |f| %>
250
+ <%= f.input :username, disabled: true, hint: 'You cannot change your username.' %>
251
+ <%= f.button :submit %>
252
+ <% end %>
253
+ ```
254
+
255
+ **Simple Form** inputs accept the same options as their corresponding input type helper in Rails:
256
+
257
+ ```erb
258
+ <%= simple_form_for @user do |f| %>
259
+ <%= f.input :date_of_birth, as: :date, start_year: Date.today.year - 90,
260
+ end_year: Date.today.year - 12, discard_day: true,
261
+ order: [:month, :year] %>
262
+ <%= f.input :accepts, as: :boolean, checked_value: 'positive', unchecked_value: 'negative' %>
263
+ <%= f.button :submit %>
264
+ <% end %>
265
+ ```
266
+
267
+ By default, **Simple Form** generates a hidden field to handle the un-checked case for boolean fields.
268
+ Passing `unchecked_value: false` in the options for boolean fields will cause this hidden field to be omitted,
269
+ following the convention in Rails. You can also specify `include_hidden: false` to skip the hidden field:
270
+
271
+ ```erb
272
+ <%= simple_form_for @user do |f| %>
273
+ <%= f.input :just_the_checked_case, as: :boolean, include_hidden: false %>
274
+ <%= f.button :submit %>
275
+ <% end %>
276
+ ```
277
+
278
+ **Simple Form** also allows you to use label, hint, input_field, error and full_error helpers
279
+ (please take a look at the rdocs for each method for more info):
280
+
281
+ ```erb
282
+ <%= simple_form_for @user do |f| %>
283
+ <%= f.label :username %>
284
+ <%= f.input_field :username %>
285
+ <%= f.hint 'No special characters, please!' %>
286
+ <%= f.error :username, id: 'user_name_error' %>
287
+ <%= f.full_error :token %>
288
+ <%= f.submit 'Save' %>
289
+ <% end %>
290
+ ```
291
+
292
+ Any extra option passed to these methods will be rendered as html option.
293
+
294
+ ### Stripping away all wrapper divs
295
+
296
+ **Simple Form** also allows you to strip away all the div wrappers around the `<input>` field that is
297
+ generated with the usual `f.input`.
298
+ The easiest way to achieve this is to use `f.input_field`.
299
+
300
+ Example:
301
+
302
+ ```ruby
303
+ simple_form_for @user do |f|
304
+ f.input_field :name
305
+ f.input_field :remember_me, as: :boolean
306
+ end
307
+ ```
308
+
309
+ ```html
310
+ <form>
311
+ ...
312
+ <input class="string required" id="user_name" maxlength="255" name="user[name]" size="255" type="text">
313
+ <input name="user[remember_me]" type="hidden" value="0">
314
+ <label class="checkbox">
315
+ <input class="boolean optional" id="user_published" name="user[remember_me]" type="checkbox" value="1">
316
+ </label>
317
+ </form>
318
+ ```
319
+
320
+ For check boxes and radio buttons you can remove the label changing `boolean_style` from default value `:nested` to `:inline`.
321
+
322
+ Example:
323
+
324
+ ```ruby
325
+ simple_form_for @user do |f|
326
+ f.input_field :name
327
+ f.input_field :remember_me, as: :boolean, boolean_style: :inline
328
+ end
329
+ ```
330
+
331
+ ```html
332
+ <form>
333
+ ...
334
+ <input class="string required" id="user_name" maxlength="255" name="user[name]" size="255" type="text">
335
+ <input name="user[remember_me]" type="hidden" value="0">
336
+ <input class="boolean optional" id="user_remember_me" name="user[remember_me]" type="checkbox" value="1">
337
+ </form>
338
+ ```
339
+
340
+ To view the actual RDocs for this, check them out here - http://rubydoc.info/github/heartcombo/simple_form/main/SimpleForm/FormBuilder:input_field
341
+
342
+ ### Collections
343
+
344
+ And what if you want to create a select containing the age from 18 to 60 in your form? You can do it
345
+ overriding the `:collection` option:
346
+
347
+ ```erb
348
+ <%= simple_form_for @user do |f| %>
349
+ <%= f.input :user %>
350
+ <%= f.input :age, collection: 18..60 %>
351
+ <%= f.button :submit %>
352
+ <% end %>
353
+ ```
354
+
355
+ Collections can be arrays or ranges, and when a `:collection` is given the `:select` input will be
356
+ rendered by default, so we don't need to pass the `as: :select` option. Other types of collection
357
+ are `:radio_buttons` and `:check_boxes`. Those are added by **Simple Form** to Rails set of form
358
+ helpers (read Extra Helpers section below for more information).
359
+
360
+ Collection inputs accept two other options beside collections:
361
+
362
+ * *label_method* => the label method to be applied to the collection to retrieve the label (use this
363
+ instead of the `text_method` option in `collection_select`)
364
+
365
+ * *value_method* => the value method to be applied to the collection to retrieve the value
366
+
367
+ Those methods are useful to manipulate the given collection. Both of these options also accept
368
+ lambda/procs in case you want to calculate the value or label in a special way eg. custom
369
+ translation. You can also define a `to_label` method on your model as **Simple Form** will search for
370
+ and use `:to_label` as a `:label_method` first if it is found.
371
+
372
+ By default, **Simple Form** will use the first item from an array as the label and the second one as the value.
373
+ If you want to change this behavior you must make it explicit, like this:
374
+
375
+ ```erb
376
+ <%= simple_form_for @user do |f| %>
377
+ <%= f.input :gender, as: :radio_buttons, collection: [['0', 'female'], ['1', 'male']], label_method: :second, value_method: :first %>
378
+ <% end %>
379
+ ```
380
+
381
+ All other options given are sent straight to the underlying Rails helper(s): [`collection_select`](http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select), [`collection_check_boxes`](http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_check_boxes), [`collection_radio_buttons`](http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_radio_buttons). For example, you can pass `prompt` and `selected` as:
382
+
383
+ ```ruby
384
+ f.input :age, collection: 18..60, prompt: "Select your age", selected: 21
385
+ ```
386
+
387
+ It may also be useful to explicitly pass a value to the optional `:selected` like above, especially if passing a collection of nested objects.
388
+
389
+ It is also possible to create grouped collection selects, that will use the html *optgroup* tags, like this:
390
+
391
+ ```ruby
392
+ f.input :country_id, collection: @continents, as: :grouped_select, group_method: :countries
393
+ ```
394
+
395
+ Grouped collection inputs accept the same `:label_method` and `:value_method` options, which will be
396
+ used to retrieve label/value attributes for the `option` tags. Besides that, you can give:
397
+
398
+ * *group_method* => the method to be called on the given collection to generate the options for
399
+ each group (required)
400
+
401
+ * *group_label_method* => the label method to be applied on the given collection to retrieve the label
402
+ for the _optgroup_ (**Simple Form** will attempt to guess the best one the same way it does with
403
+ `:label_method`)
404
+
405
+ ### Priority
406
+
407
+ **Simple Form** also supports `:time_zone` and `:country`. When using such helpers, you can give
408
+ `:priority` as an option to select which time zones and/or countries should be given higher priority:
409
+
410
+ ```ruby
411
+ f.input :residence_country, priority: [ "Brazil" ]
412
+ f.input :time_zone, priority: /US/
413
+ ```
414
+
415
+ Those values can also be configured with a default value to be used on the site through the
416
+ `SimpleForm.country_priority` and `SimpleForm.time_zone_priority` helpers.
417
+
418
+ Note: While using `country_select` if you want to restrict to only a subset of countries for a specific
419
+ drop down then you may use the `:collection` option:
420
+
421
+ ```ruby
422
+ f.input :shipping_country, priority: [ "Brazil" ], collection: [ "Australia", "Brazil", "New Zealand"]
423
+ ```
424
+
425
+ ### Associations
426
+
427
+ To deal with associations, **Simple Form** can generate select inputs, a series of radios buttons or checkboxes.
428
+ Lets see how it works: imagine you have a user model that belongs to a company and `has_and_belongs_to_many`
429
+ roles. The structure would be something like:
430
+
431
+ ```ruby
432
+ class User < ActiveRecord::Base
433
+ belongs_to :company
434
+ has_and_belongs_to_many :roles
435
+ end
436
+
437
+ class Company < ActiveRecord::Base
438
+ has_many :users
439
+ end
440
+
441
+ class Role < ActiveRecord::Base
442
+ has_and_belongs_to_many :users
443
+ end
444
+ ```
445
+
446
+ Now we have the user form:
447
+
448
+ ```erb
449
+ <%= simple_form_for @user do |f| %>
450
+ <%= f.input :name %>
451
+ <%= f.association :company %>
452
+ <%= f.association :roles %>
453
+ <%= f.button :submit %>
454
+ <% end %>
455
+ ```
456
+
457
+ Simple enough, right? This is going to render a `:select` input for choosing the `:company`, and another
458
+ `:select` input with `:multiple` option for the `:roles`. You can, of course, change it to use radio
459
+ buttons and checkboxes as well:
460
+
461
+ ```ruby
462
+ f.association :company, as: :radio_buttons
463
+ f.association :roles, as: :check_boxes
464
+ ```
465
+
466
+ The association helper just invokes `input` under the hood, so all options available to `:select`,
467
+ `:radio_buttons` and `:check_boxes` are also available to association. Additionally, you can specify
468
+ the collection by hand, all together with the prompt:
469
+
470
+ ```ruby
471
+ f.association :company, collection: Company.active.order(:name), prompt: "Choose a Company"
472
+ ```
473
+
474
+ In case you want to declare different labels and values:
475
+
476
+ ```ruby
477
+ f.association :company, label_method: :company_name, value_method: :id, include_blank: false
478
+ ```
479
+
480
+ Please note that the association helper is currently only tested with Active Record. It currently
481
+ does not work well with Mongoid and depending on the ORM you're using your mileage may vary.
482
+
483
+ ### Buttons
484
+
485
+ All web forms need buttons, right? **Simple Form** wraps them in the DSL, acting like a proxy:
486
+
487
+ ```erb
488
+ <%= simple_form_for @user do |f| %>
489
+ <%= f.input :name %>
490
+ <%= f.button :submit %>
491
+ <% end %>
492
+ ```
493
+
494
+ The above will simply call submit. You choose to use it or not, it's just a question of taste.
495
+
496
+ The button method also accepts optional parameters, that are delegated to the underlying submit call:
497
+
498
+ ```erb
499
+ <%= f.button :submit, "Custom Button Text", class: "my-button" %>
500
+ ```
501
+
502
+ To create a `<button>` element, use the following syntax:
503
+
504
+ ```erb
505
+ <%= f.button :button, "Custom Button Text" %>
506
+
507
+ <%= f.button :button do %>
508
+ Custom Button Text
509
+ <% end %>
510
+ ```
511
+
512
+ ### Wrapping Rails Form Helpers
513
+
514
+ Say you wanted to use a rails form helper but still wrap it in **Simple Form** goodness? You can, by
515
+ calling input with a block like so:
516
+
517
+ ```erb
518
+ <%= f.input :role do %>
519
+ <%= f.select :role, Role.all.map { |r| [r.name, r.id, { class: r.company.id }] }, include_blank: true %>
520
+ <% end %>
521
+ ```
522
+
523
+ In the above example, we're taking advantage of Rails 3's select method that allows us to pass in a
524
+ hash of additional attributes for each option.
525
+
526
+ ### Extra helpers
527
+
528
+ **Simple Form** also comes with some extra helpers you can use inside rails default forms without relying
529
+ on `simple_form_for` helper. They are listed below.
530
+
531
+ #### Simple Fields For
532
+
533
+ Wrapper to use **Simple Form** inside a default rails form. It works in the same way that the `fields_for`
534
+ Rails helper, but change the builder to use the `SimpleForm::FormBuilder`.
535
+
536
+ ```ruby
537
+ form_for @user do |f|
538
+ f.simple_fields_for :posts do |posts_form|
539
+ # Here you have all simple_form methods available
540
+ posts_form.input :title
541
+ end
542
+ end
543
+ ```
544
+
545
+ #### Collection Radio Buttons
546
+
547
+ Creates a collection of radio inputs with labels associated (same API as `collection_select`):
548
+
549
+ ```ruby
550
+ form_for @user do |f|
551
+ f.collection_radio_buttons :options, [[true, 'Yes'], [false, 'No']], :first, :last
552
+ end
553
+ ```
554
+
555
+ ```html
556
+ <input id="user_options_true" name="user[options]" type="radio" value="true" />
557
+ <label class="collection_radio_buttons" for="user_options_true">Yes</label>
558
+ <input id="user_options_false" name="user[options]" type="radio" value="false" />
559
+ <label class="collection_radio_buttons" for="user_options_false">No</label>
560
+ ```
561
+
562
+ #### Collection Check Boxes
563
+
564
+ Creates a collection of checkboxes with labels associated (same API as `collection_select`):
565
+
566
+ ```ruby
567
+ form_for @user do |f|
568
+ f.collection_check_boxes :options, [[true, 'Yes'], [false, 'No']], :first, :last
569
+ end
570
+ ```
571
+
572
+ ```html
573
+ <input name="user[options][]" type="hidden" value="" />
574
+ <input id="user_options_true" name="user[options][]" type="checkbox" value="true" />
575
+ <label class="collection_check_box" for="user_options_true">Yes</label>
576
+ <input name="user[options][]" type="hidden" value="" />
577
+ <input id="user_options_false" name="user[options][]" type="checkbox" value="false" />
578
+ <label class="collection_check_box" for="user_options_false">No</label>
579
+ ```
580
+
581
+ To use this with associations in your model, you can do the following:
582
+
583
+ ```ruby
584
+ form_for @user do |f|
585
+ f.collection_check_boxes :role_ids, Role.all, :id, :name # using :roles here is not going to work.
586
+ end
587
+ ```
588
+
589
+ To add a CSS class to the label item, you can use the `item_label_class` option:
590
+
591
+ ```ruby
592
+ f.collection_check_boxes :role_ids, Role.all, :id, :name, item_label_class: 'my-custom-class'
593
+ ```
594
+
595
+ ## Available input types and defaults for each column type
596
+
597
+ The following table shows the html element you will get for each attribute
598
+ according to its database definition. These defaults can be changed by
599
+ specifying the helper method in the column `Mapping` as the `as:` option.
600
+
601
+ Mapping | Generated HTML Element | Database Column Type
602
+ --------------- |--------------------------------------|---------------------
603
+ `boolean` | `input[type=checkbox]` | `boolean`
604
+ `string` | `input[type=text]` | `string`
605
+ `citext` | `input[type=text]` | `citext`
606
+ `email` | `input[type=email]` | `string` with `name =~ /email/`
607
+ `url` | `input[type=url]` | `string` with `name =~ /url/`
608
+ `tel` | `input[type=tel]` | `string` with `name =~ /phone/`
609
+ `password` | `input[type=password]` | `string` with `name =~ /password/`
610
+ `search` | `input[type=search]` | -
611
+ `uuid` | `input[type=text]` | `uuid`
612
+ `color` | `input[type=color]` | `string`
613
+ `text` | `textarea` | `text`
614
+ `hstore` | `textarea` | `hstore`
615
+ `json` | `textarea` | `json`
616
+ `jsonb` | `textarea` | `jsonb`
617
+ `file` | `input[type=file]` | `string` responding to file methods
618
+ `hidden` | `input[type=hidden]` | -
619
+ `integer` | `input[type=number]` | `integer`
620
+ `float` | `input[type=number]` | `float`
621
+ `decimal` | `input[type=number]` | `decimal`
622
+ `range` | `input[type=range]` | -
623
+ `datetime` | `datetime select` | `datetime/timestamp`
624
+ `date` | `date select` | `date`
625
+ `time` | `time select` | `time`
626
+ `weekday` | `select` (weekdays as options) | -
627
+ `select` | `select` | `belongs_to`/`has_many`/`has_and_belongs_to_many` associations
628
+ `radio_buttons` | collection of `input[type=radio]` | `belongs_to` associations
629
+ `check_boxes` | collection of `input[type=checkbox]` | `has_many`/`has_and_belongs_to_many` associations
630
+ `country` | `select` (countries as options) | `string` with `name =~ /country/`
631
+ `time_zone` | `select` (timezones as options) | `string` with `name =~ /time_zone/`
632
+ `rich_text_area`| `trix-editor` | -
633
+
634
+ ## Custom inputs
635
+
636
+ It is very easy to add custom inputs to **Simple Form**. For instance, if you want to add a custom input
637
+ that extends the string one, you just need to add this file:
638
+
639
+ ```ruby
640
+ # app/inputs/currency_input.rb
641
+ class CurrencyInput < SimpleForm::Inputs::Base
642
+ def input(wrapper_options)
643
+ merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
644
+
645
+ "$ #{@builder.text_field(attribute_name, merged_input_options)}".html_safe
646
+ end
647
+ end
648
+ ```
649
+
650
+ And use it in your views:
651
+
652
+ ```ruby
653
+ f.input :money, as: :currency
654
+ ```
655
+ Note, you may have to create the `app/inputs/` directory and restart your webserver.
656
+
657
+ You can also redefine existing **Simple Form** inputs by creating a new class with the same name. For
658
+ instance, if you want to wrap date/time/datetime in a div, you can do:
659
+
660
+ ```ruby
661
+ # app/inputs/date_time_input.rb
662
+ class DateTimeInput < SimpleForm::Inputs::DateTimeInput
663
+ def input(wrapper_options)
664
+ template.content_tag(:div, super)
665
+ end
666
+ end
667
+ ```
668
+
669
+ Or if you want to add a class to all the select fields you can do:
670
+
671
+ ```ruby
672
+ # app/inputs/collection_select_input.rb
673
+ class CollectionSelectInput < SimpleForm::Inputs::CollectionSelectInput
674
+ def input_html_classes
675
+ super.push('chosen')
676
+ end
677
+ end
678
+ ```
679
+
680
+ If needed, you can namespace your custom inputs in a module and tell **Simple Form** to look for
681
+ their definitions in this module. This can avoid conflicts with other form libraries (like Formtastic) that look up
682
+ the global context to find inputs definition too.
683
+
684
+ ```ruby
685
+ # app/inputs/custom_inputs/numeric_input
686
+ module CustomInputs
687
+ class NumericInput < SimpleForm::Inputs::NumericInput
688
+ def input_html_classes
689
+ super.push('no-spinner')
690
+ end
691
+ end
692
+ end
693
+ ```
694
+
695
+ And in the **SimpleForm** initializer :
696
+
697
+ ```ruby
698
+ # config/simple_form.rb
699
+ config.custom_inputs_namespaces << "CustomInputs"
700
+ ```
701
+
702
+ ## Custom form builder
703
+
704
+ You can create a custom form builder that uses **Simple Form**.
705
+
706
+ Create a helper method that calls `simple_form_for` with a custom builder:
707
+
708
+ ```ruby
709
+ def custom_form_for(object, *args, &block)
710
+ options = args.extract_options!
711
+ simple_form_for(object, *(args << options.merge(builder: CustomFormBuilder)), &block)
712
+ end
713
+ ```
714
+
715
+ Create a form builder class that inherits from `SimpleForm::FormBuilder`.
716
+
717
+ ```ruby
718
+ class CustomFormBuilder < SimpleForm::FormBuilder
719
+ def input(attribute_name, options = {}, &block)
720
+ super(attribute_name, options.merge(label: false), &block)
721
+ end
722
+ end
723
+ ```
724
+
725
+ ## I18n
726
+
727
+ **Simple Form** uses all power of I18n API to lookup labels, hints, prompts and placeholders. To customize your
728
+ forms you can create a locale file like this:
729
+
730
+ ```yaml
731
+ en:
732
+ simple_form:
733
+ labels:
734
+ user:
735
+ username: 'User name'
736
+ password: 'Password'
737
+ hints:
738
+ user:
739
+ username: 'User name to sign in.'
740
+ password: 'No special characters, please.'
741
+ placeholders:
742
+ user:
743
+ username: 'Your username'
744
+ password: '****'
745
+ include_blanks:
746
+ user:
747
+ age: 'Rather not say'
748
+ prompts:
749
+ user:
750
+ role: 'Select your role'
751
+ ```
752
+
753
+ And your forms will use this information to render the components for you.
754
+
755
+ **Simple Form** also lets you be more specific, separating lookups through actions.
756
+ Let's say you want a different label for new and edit actions, the locale file would
757
+ be something like:
758
+
759
+ ```yaml
760
+ en:
761
+ simple_form:
762
+ labels:
763
+ user:
764
+ username: 'User name'
765
+ password: 'Password'
766
+ edit:
767
+ username: 'Change user name'
768
+ password: 'Change password'
769
+ ```
770
+
771
+ This way **Simple Form** will figure out the right translation for you, based on the action being
772
+ rendered. And to be a little bit DRYer with your locale file, you can specify defaults for all
773
+ models under the 'defaults' key:
774
+
775
+ ```yaml
776
+ en:
777
+ simple_form:
778
+ labels:
779
+ defaults:
780
+ username: 'User name'
781
+ password: 'Password'
782
+ new:
783
+ username: 'Choose a user name'
784
+ hints:
785
+ defaults:
786
+ username: 'User name to sign in.'
787
+ password: 'No special characters, please.'
788
+ placeholders:
789
+ defaults:
790
+ username: 'Your username'
791
+ password: '****'
792
+ ```
793
+
794
+ **Simple Form** will always look for a default attribute translation under the "defaults" key if no
795
+ specific is found inside the model key.
796
+
797
+ In addition, **Simple Form** will fallback to default `human_attribute_name` from Rails when no other
798
+ translation is found for labels. Finally, you can also overwrite any label, hint or placeholder
799
+ inside your view, just by passing the option manually. This way the I18n lookup will be skipped.
800
+
801
+ For `:prompt` and `:include_blank` the I18n lookup is optional and to enable it is necessary to pass
802
+ `:translate` as value.
803
+
804
+ ```ruby
805
+ f.input :role, prompt: :translate
806
+ ```
807
+
808
+ **Simple Form** also has support for translating options in collection helpers. For instance, given a
809
+ User with a `:role` attribute, you might want to create a select box showing translated labels
810
+ that would post either `:admin` or `:editor` as value. With **Simple Form** you could create an input
811
+ like this:
812
+
813
+ ```ruby
814
+ f.input :role, collection: [:admin, :editor]
815
+ ```
816
+
817
+ And **Simple Form** will try a lookup like this in your locale file, to find the right labels to show:
818
+
819
+ ```yaml
820
+ en:
821
+ simple_form:
822
+ options:
823
+ user:
824
+ role:
825
+ admin: 'Administrator'
826
+ editor: 'Editor'
827
+ ```
828
+
829
+ You can also use the `defaults` key as you would do with labels, hints and placeholders. It is
830
+ important to notice that **Simple Form** will only do the lookup for options if you give a collection
831
+ composed of symbols only. This is to avoid constant lookups to I18n.
832
+
833
+ It's also possible to translate buttons, using Rails' built-in I18n support:
834
+
835
+ ```yaml
836
+ en:
837
+ helpers:
838
+ submit:
839
+ user:
840
+ create: "Add %{model}"
841
+ update: "Save Changes"
842
+ ```
843
+
844
+ There are other options that can be configured through I18n API, such as required text and boolean.
845
+ Be sure to check our locale file or the one copied to your application after you run
846
+ `rails generate simple_form:install`.
847
+
848
+ It should be noted that translations for labels, hints and placeholders for a namespaced model, e.g.
849
+ `Admin::User`, should be placed under `admin_user`, not under `admin/user`. This is different from
850
+ how translations for namespaced model and attribute names are defined:
851
+
852
+ ```yaml
853
+ en:
854
+ activerecord:
855
+ models:
856
+ admin/user: User
857
+ attributes:
858
+ admin/user:
859
+ name: Name
860
+ ```
861
+
862
+ They should be placed under `admin/user`. Form labels, hints and placeholders for those attributes,
863
+ though, should be placed under `admin_user`:
864
+
865
+ ```yaml
866
+ en:
867
+ simple_form:
868
+ labels:
869
+ admin_user:
870
+ name: Name
871
+ ```
872
+
873
+ This difference exists because **Simple Form** relies on `object_name` provided by Rails'
874
+ FormBuilder to determine the translation path for a given object instead of `i18n_key` from the
875
+ object itself. Thus, similarly, if a form for an `Admin::User` object is defined by calling
876
+ `simple_form_for @admin_user, as: :some_user`, **Simple Form** will look for translations
877
+ under `some_user` instead of `admin_user`.
878
+
879
+ When translating `simple_fields_for` attributes be sure to use the same name you pass to it, e.g. `simple_fields_for :posts` should be placed under `posts` not `post`:
880
+
881
+ ```yaml
882
+ en:
883
+ simple_form:
884
+ labels:
885
+ posts:
886
+ title: 'Post title'
887
+ hints:
888
+ posts:
889
+ title: 'A good title'
890
+ placeholders:
891
+ posts:
892
+ title: 'Once upon a time...'
893
+ ```
894
+
895
+ ## Configuration
896
+
897
+ **Simple Form** has several configuration options. You can read and change them in the initializer
898
+ created by **Simple Form**, so if you haven't executed the command below yet, please do:
899
+
900
+ `rails generate simple_form:install`
901
+
902
+ ### The wrappers API
903
+
904
+ With **Simple Form** you can configure how your components will be rendered using the wrappers API.
905
+ The syntax looks like this:
906
+
907
+ ```ruby
908
+ config.wrappers tag: :div, class: :input,
909
+ error_class: :field_with_errors,
910
+ valid_class: :field_without_errors do |b|
911
+
912
+ # Form extensions
913
+ b.use :html5
914
+ b.optional :pattern
915
+ b.use :maxlength
916
+ b.use :placeholder
917
+ b.use :readonly
918
+
919
+ # Form components
920
+ b.use :label_input
921
+ b.use :hint, wrap_with: { tag: :span, class: :hint }
922
+ b.use :error, wrap_with: { tag: :span, class: :error }
923
+ end
924
+ ```
925
+
926
+ The _Form components_ will generate the form tags like labels, inputs, hints or errors contents.
927
+ The available components are:
928
+
929
+ ```ruby
930
+ :label # The <label> tag alone
931
+ :input # The <input> tag alone
932
+ :label_input # The <label> and the <input> tags
933
+ :hint # The hint for the input
934
+ :error # The error for the input
935
+ ```
936
+
937
+ The _Form extensions_ are used to generate some attributes or perform some lookups on the model to
938
+ add extra information to your components.
939
+
940
+ You can create new _Form components_ using the wrappers API as in the following example:
941
+
942
+ ```ruby
943
+ config.wrappers do |b|
944
+ b.use :placeholder
945
+ b.use :label_input
946
+ b.wrapper tag: :div, class: 'separator' do |component|
947
+ component.use :hint, wrap_with: { tag: :span, class: :hint }
948
+ component.use :error, wrap_with: { tag: :span, class: :error }
949
+ end
950
+ end
951
+ ```
952
+
953
+ this will wrap the hint and error components within a `div` tag using the class `'separator'`.
954
+
955
+ You can customize _Form components_ passing options to them:
956
+
957
+ ```ruby
958
+ config.wrappers do |b|
959
+ b.use :label_input, class: 'label-input-class', error_class: 'is-invalid', valid_class: 'is-valid'
960
+ end
961
+ ```
962
+
963
+ This sets the input and label classes to `'label-input-class'` and will set the class `'is-invalid'`
964
+ if the input has errors and `'is-valid'` if the input is valid.
965
+
966
+ If you want to customize the custom _Form components_ on demand you can give it a name like this:
967
+
968
+ ```ruby
969
+ config.wrappers do |b|
970
+ b.use :placeholder
971
+ b.use :label_input
972
+ b.wrapper :my_wrapper, tag: :div, class: 'separator', html: { id: 'my_wrapper_id' } do |component|
973
+ component.use :hint, wrap_with: { tag: :span, class: :hint }
974
+ component.use :error, wrap_with: { tag: :span, class: :error }
975
+ end
976
+ end
977
+ ```
978
+
979
+ and now you can pass options to your `input` calls to customize the `:my_wrapper` _Form component_.
980
+
981
+ ```ruby
982
+ # Completely turns off the custom wrapper
983
+ f.input :name, my_wrapper: false
984
+
985
+ # Configure the html
986
+ f.input :name, my_wrapper_html: { id: 'special_id' }
987
+
988
+ # Configure the tag
989
+ f.input :name, my_wrapper_tag: :p
990
+ ```
991
+
992
+ You can also define more than one wrapper and pick one to render in a specific form or input.
993
+ To define another wrapper you have to give it a name, as the follow:
994
+
995
+ ```ruby
996
+ config.wrappers :small do |b|
997
+ b.use :placeholder
998
+ b.use :label_input
999
+ end
1000
+ ```
1001
+
1002
+ and use it in this way:
1003
+
1004
+ ```ruby
1005
+ # Specifying to whole form
1006
+ simple_form_for @user, wrapper: :small do |f|
1007
+ f.input :name
1008
+ end
1009
+
1010
+ # Specifying to one input
1011
+ simple_form_for @user do |f|
1012
+ f.input :name, wrapper: :small
1013
+ end
1014
+ ```
1015
+
1016
+ **Simple Form** also allows you to use optional elements. For instance, let's suppose you want to use
1017
+ hints or placeholders, but you don't want them to be generated automatically. You can set their
1018
+ default values to `false` or use the `optional` method. Is preferable to use the `optional` syntax:
1019
+
1020
+ ```ruby
1021
+ config.wrappers placeholder: false do |b|
1022
+ b.use :placeholder
1023
+ b.use :label_input
1024
+ b.wrapper tag: :div, class: 'separator' do |component|
1025
+ component.optional :hint, wrap_with: { tag: :span, class: :hint }
1026
+ component.use :error, wrap_with: { tag: :span, class: :error }
1027
+ end
1028
+ end
1029
+ ```
1030
+
1031
+ By setting it as `optional`, a hint will only be generated when `hint: true` is explicitly used.
1032
+ The same for placeholder.
1033
+
1034
+ It is also possible to give the option `:unless_blank` to the wrapper if you want to render it only
1035
+ when the content is present.
1036
+
1037
+ ```ruby
1038
+ b.wrapper tag: :span, class: 'hint', unless_blank: true do |component|
1039
+ component.optional :hint
1040
+ end
1041
+ ```
1042
+
1043
+ ## Custom Components
1044
+
1045
+ When you use custom wrappers, you might also be looking for a way to add custom components to your
1046
+ wrapper. The default components are:
1047
+
1048
+ ```ruby
1049
+ :label # The <label> tag alone
1050
+ :input # The <input> tag alone
1051
+ :label_input # The <label> and the <input> tags
1052
+ :hint # The hint for the input
1053
+ :error # The error for the input
1054
+ ```
1055
+
1056
+ A custom component might be interesting for you if your views look something like this:
1057
+
1058
+ ```erb
1059
+ <%= simple_form_for @blog do |f| %>
1060
+ <div class="row">
1061
+ <div class="span1 number">
1062
+ 1
1063
+ </div>
1064
+ <div class="span8">
1065
+ <%= f.input :title %>
1066
+ </div>
1067
+ </div>
1068
+ <div class="row">
1069
+ <div class="span1 number">
1070
+ 2
1071
+ </div>
1072
+ <div class="span8">
1073
+ <%= f.input :body, as: :text %>
1074
+ </div>
1075
+ </div>
1076
+ <% end %>
1077
+ ```
1078
+
1079
+ A cleaner method to create your views would be:
1080
+
1081
+ ```erb
1082
+ <%= simple_form_for @blog, wrapper: :with_numbers do |f| %>
1083
+ <%= f.input :title, number: 1 %>
1084
+ <%= f.input :body, as: :text, number: 2 %>
1085
+ <% end %>
1086
+ ```
1087
+
1088
+ To use the number option on the input, first, tells to Simple Form the place where the components
1089
+ will be:
1090
+
1091
+ ``` ruby
1092
+ # config/initializers/simple_form.rb
1093
+ Dir[Rails.root.join('lib/components/**/*.rb')].each { |f| require f }
1094
+ ```
1095
+
1096
+ Create a new component within the path specified above:
1097
+
1098
+ ```ruby
1099
+ # lib/components/numbers_component.rb
1100
+ module NumbersComponent
1101
+ # To avoid deprecation warning, you need to make the wrapper_options explicit
1102
+ # even when they won't be used.
1103
+ def number(wrapper_options = nil)
1104
+ @number ||= begin
1105
+ options[:number].to_s.html_safe if options[:number].present?
1106
+ end
1107
+ end
1108
+ end
1109
+
1110
+ SimpleForm.include_component(NumbersComponent)
1111
+ ```
1112
+
1113
+ Finally, add a new wrapper to the config/initializers/simple_form.rb file:
1114
+
1115
+ ```ruby
1116
+ config.wrappers :with_numbers, tag: 'div', class: 'row', error_class: 'error' do |b|
1117
+ b.use :html5
1118
+ b.use :number, wrap_with: { tag: 'div', class: 'span1 number' }
1119
+ b.wrapper tag: 'div', class: 'span8' do |ba|
1120
+ ba.use :placeholder
1121
+ ba.use :label
1122
+ ba.use :input
1123
+ ba.use :error, wrap_with: { tag: 'span', class: 'help-inline' }
1124
+ ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
1125
+ end
1126
+ end
1127
+ ```
1128
+
1129
+ ## HTML 5 Notice
1130
+
1131
+ By default, **Simple Form** will generate input field types and attributes that are supported in HTML5,
1132
+ but are considered invalid HTML for older document types such as HTML4 or XHTML1.0. The HTML5
1133
+ extensions include the new field types such as email, number, search, url, tel, and the new
1134
+ attributes such as required, autofocus, maxlength, min, max, step.
1135
+
1136
+ Most browsers will not care, but some of the newer ones - in particular Chrome 10+ - use the
1137
+ required attribute to force a value into an input and will prevent form submission without it.
1138
+ Depending on the design of the application this may or may not be desired. In many cases it can
1139
+ break existing UI's.
1140
+
1141
+ It is possible to disable all HTML 5 extensions in **Simple Form** by removing the `html5`
1142
+ component from the wrapper used to render the inputs.
1143
+
1144
+ For example, change:
1145
+
1146
+ ```ruby
1147
+ config.wrappers tag: :div do |b|
1148
+ b.use :html5
1149
+
1150
+ b.use :label_input
1151
+ end
1152
+ ```
1153
+
1154
+ To:
1155
+
1156
+ ```ruby
1157
+ config.wrappers tag: :div do |b|
1158
+ b.use :label_input
1159
+ end
1160
+ ```
1161
+
1162
+ If you want to have all other HTML 5 features, such as the new field types, you can disable only
1163
+ the browser validation:
1164
+
1165
+ ```ruby
1166
+ SimpleForm.browser_validations = false # default is true
1167
+ ```
1168
+
1169
+ This option adds a new `novalidate` property to the form, instructing it to skip all HTML 5
1170
+ validation. The inputs will still be generated with the required and other attributes, that might
1171
+ help you to use some generic javascript validation.
1172
+
1173
+ You can also add `novalidate` to a specific form by setting the option on the form itself:
1174
+
1175
+ ```erb
1176
+ <%= simple_form_for(resource, html: { novalidate: true }) do |form| %>
1177
+ ```
1178
+
1179
+ Please notice that none of the configurations above will disable the `placeholder` component,
1180
+ which is an HTML 5 feature. We believe most of the newest browsers are handling this attribute
1181
+ just fine, and if they aren't, any plugin you use would take care of applying the placeholder.
1182
+ In any case, you can disable it if you really want to, by removing the placeholder component
1183
+ from the components list in the **Simple Form** configuration file.
1184
+
1185
+ HTML 5 date / time inputs are not generated by **Simple Form** by default, so using `date`,
1186
+ `time` or `datetime` will all generate select boxes using normal Rails helpers. We believe
1187
+ browsers are not totally ready for these yet, but you can easily opt-in on a per-input basis
1188
+ by passing the html5 option:
1189
+
1190
+ ```erb
1191
+ <%= f.input :expires_at, as: :date, html5: true %>
1192
+ ```
1193
+
1194
+ ## Using non Active Record objects
1195
+
1196
+ There are few ways to build forms with objects that don't inherit from Active Record, as
1197
+ follows:
1198
+
1199
+ You can include the module `ActiveModel::Model`.
1200
+
1201
+ ```ruby
1202
+ class User
1203
+ include ActiveModel::Model
1204
+
1205
+ attr_accessor :id, :name
1206
+ end
1207
+ ```
1208
+
1209
+ If you are using Presenters or Decorators that inherit from `SimpleDelegator` you can delegate
1210
+ it to the model.
1211
+
1212
+ ```ruby
1213
+ class UserPresenter < SimpleDelegator
1214
+ # Without that, Simple Form will consider the user model as the object.
1215
+ def to_model
1216
+ self
1217
+ end
1218
+ end
1219
+ ```
1220
+
1221
+ You can define all methods required by the helpers.
1222
+
1223
+ ```ruby
1224
+ class User
1225
+ extend ActiveModel::Naming
1226
+
1227
+ attr_accessor :id, :name
1228
+
1229
+ def to_model
1230
+ self
1231
+ end
1232
+
1233
+ def to_key
1234
+ id
1235
+ end
1236
+
1237
+ def persisted?
1238
+ false
1239
+ end
1240
+ end
1241
+ ```
1242
+
1243
+ To have SimpleForm infer the attributes' types, you can provide
1244
+ `#has_attribute?` and `#type_for_attribute` methods.
1245
+ The later should return an object that responds to `#type`
1246
+ with the attribute type. This is useful for generating
1247
+ the correct input types (eg: checkboxes for booleans).
1248
+
1249
+ ```ruby
1250
+ class User < Struct.new(:id, :name, :age, :registered)
1251
+ def to_model
1252
+ self
1253
+ end
1254
+
1255
+ def model_name
1256
+ OpenStruct.new(param_key: "user")
1257
+ end
1258
+
1259
+ def to_key
1260
+ id
1261
+ end
1262
+
1263
+ def persisted?
1264
+ id.present?
1265
+ end
1266
+
1267
+ def has_attribute?(attr_name)
1268
+ %w(id name age registered).include?(attr_name.to_s)
1269
+ end
1270
+
1271
+ def type_for_attribute(attr_name)
1272
+ case attr_name.to_s
1273
+ when "id" then OpenStruct.new(type: :integer)
1274
+ when "name" then OpenStruct.new(type: :string)
1275
+ when "age" then OpenStruct.new(type: :integer)
1276
+ when "registered" then OpenStruct.new(type: :boolean)
1277
+ end
1278
+ end
1279
+ end
1280
+ ```
1281
+
1282
+ If your object doesn't implement those methods, you must make explicit it when you are
1283
+ building the form
1284
+
1285
+ ```ruby
1286
+ class User
1287
+ attr_accessor :id, :name
1288
+
1289
+ # The only method required to use the f.submit helper.
1290
+ def persisted?
1291
+ false
1292
+ end
1293
+ end
1294
+ ```
1295
+
1296
+ ```erb
1297
+ <%= simple_form_for(@user, as: :user, method: :post, url: users_path) do |f| %>
1298
+ <%= f.input :name %>
1299
+ <%= f.submit 'New user' %>
1300
+ <% end %>
1301
+ ```
1302
+
1303
+ ## Information
1304
+
1305
+ ### RDocs
1306
+
1307
+ You can view the **Simple Form** documentation in RDoc format here:
1308
+
1309
+ http://rubydoc.info/github/heartcombo/simple_form/main/frames
1310
+
1311
+ ### Supported Ruby / Rails versions
1312
+
1313
+ We intend to maintain support for all Ruby / Rails versions that haven't reached end-of-life.
1314
+
1315
+ For more information about specific versions please check [Ruby](https://www.ruby-lang.org/en/downloads/branches/)
1316
+ and [Rails](https://guides.rubyonrails.org/maintenance_policy.html) maintenance policies, and our test matrix.
1317
+
1318
+ ### Bug reports
1319
+
1320
+ If you discover any bugs, feel free to create an issue on GitHub. Please add as much information as
1321
+ possible to help us in fixing the potential bug. We also encourage you to help even more by forking and
1322
+ sending us a pull request.
1323
+
1324
+ https://github.com/heartcombo/simple_form/issues
1325
+
1326
+ If you have discovered a security related bug, please do NOT use the GitHub issue tracker. Send an e-mail to heartcombo.oss@gmail.com.
1327
+
1328
+ ## License
1329
+
1330
+ MIT License.
1331
+ Copyright 2020-CURRENT Rafael França, Carlos Antonio da Silva.
1332
+ Copyright 2009-2019 Plataformatec.
1333
+
1334
+ The Simple Form logo is licensed under [Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License](https://creativecommons.org/licenses/by-nc-nd/4.0/).