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