simple_form 1.5.2 → 2.0.0.rc

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 (105) hide show
  1. data/CHANGELOG.md +224 -0
  2. data/MIT-LICENSE +1 -1
  3. data/README.md +817 -0
  4. data/lib/generators/simple_form/install_generator.rb +15 -1
  5. data/lib/generators/simple_form/templates/README +12 -0
  6. data/lib/generators/simple_form/templates/config/initializers/simple_form.rb.tt +173 -0
  7. data/lib/simple_form.rb +109 -43
  8. data/lib/simple_form/action_view_extensions/builder.rb +158 -53
  9. data/lib/simple_form/action_view_extensions/form_helper.rb +29 -22
  10. data/lib/simple_form/components.rb +11 -1
  11. data/lib/simple_form/components/errors.rb +6 -24
  12. data/lib/simple_form/components/hints.rb +7 -21
  13. data/lib/simple_form/components/html5.rb +26 -0
  14. data/lib/simple_form/components/labels.rb +15 -13
  15. data/lib/simple_form/components/maxlength.rb +41 -0
  16. data/lib/simple_form/components/min_max.rb +49 -0
  17. data/lib/simple_form/components/pattern.rb +34 -0
  18. data/lib/simple_form/components/placeholders.rb +5 -17
  19. data/lib/simple_form/components/readonly.rb +22 -0
  20. data/lib/simple_form/core_ext/hash.rb +16 -0
  21. data/lib/simple_form/error_notification.rb +8 -1
  22. data/lib/simple_form/form_builder.rb +86 -22
  23. data/lib/simple_form/helpers.rb +7 -4
  24. data/lib/simple_form/helpers/autofocus.rb +11 -0
  25. data/lib/simple_form/helpers/disabled.rb +15 -0
  26. data/lib/simple_form/helpers/readonly.rb +15 -0
  27. data/lib/simple_form/helpers/required.rb +7 -10
  28. data/lib/simple_form/helpers/validators.rb +4 -4
  29. data/lib/simple_form/inputs.rb +17 -13
  30. data/lib/simple_form/inputs/base.rb +50 -81
  31. data/lib/simple_form/inputs/boolean_input.rb +43 -4
  32. data/lib/simple_form/inputs/collection_check_boxes_input.rb +21 -0
  33. data/lib/simple_form/inputs/collection_input.rb +27 -13
  34. data/lib/simple_form/inputs/collection_radio_buttons_input.rb +69 -0
  35. data/lib/simple_form/inputs/collection_select_input.rb +14 -0
  36. data/lib/simple_form/inputs/date_time_input.rb +2 -2
  37. data/lib/simple_form/inputs/grouped_collection_select_input.rb +41 -0
  38. data/lib/simple_form/inputs/hidden_input.rb +3 -6
  39. data/lib/simple_form/inputs/numeric_input.rb +3 -51
  40. data/lib/simple_form/inputs/password_input.rb +1 -2
  41. data/lib/simple_form/inputs/priority_input.rb +2 -2
  42. data/lib/simple_form/inputs/range_input.rb +1 -3
  43. data/lib/simple_form/inputs/string_input.rb +6 -8
  44. data/lib/simple_form/inputs/text_input.rb +1 -2
  45. data/lib/simple_form/version.rb +1 -1
  46. data/lib/simple_form/wrappers.rb +8 -0
  47. data/lib/simple_form/wrappers/builder.rb +75 -0
  48. data/lib/simple_form/wrappers/many.rb +68 -0
  49. data/lib/simple_form/wrappers/root.rb +34 -0
  50. data/lib/simple_form/wrappers/single.rb +18 -0
  51. data/test/action_view_extensions/builder_test.rb +195 -100
  52. data/test/action_view_extensions/form_helper_test.rb +24 -2
  53. data/test/components/label_test.rb +20 -5
  54. data/test/form_builder/association_test.rb +167 -0
  55. data/test/form_builder/button_test.rb +28 -0
  56. data/test/{error_notification_test.rb → form_builder/error_notification_test.rb} +2 -1
  57. data/test/form_builder/error_test.rb +101 -0
  58. data/test/form_builder/general_test.rb +348 -0
  59. data/test/form_builder/hint_test.rb +115 -0
  60. data/test/form_builder/input_field_test.rb +51 -0
  61. data/test/form_builder/label_test.rb +51 -0
  62. data/test/form_builder/wrapper_test.rb +140 -0
  63. data/test/generators/simple_form_generator_test.rb +32 -0
  64. data/test/inputs/boolean_input_test.rb +91 -0
  65. data/test/inputs/collection_check_boxes_input_test.rb +224 -0
  66. data/test/inputs/collection_radio_buttons_input_test.rb +326 -0
  67. data/test/inputs/collection_select_input_test.rb +241 -0
  68. data/test/inputs/datetime_input_test.rb +85 -0
  69. data/test/inputs/disabled_test.rb +38 -0
  70. data/test/inputs/discovery_test.rb +61 -0
  71. data/test/inputs/file_input_test.rb +16 -0
  72. data/test/inputs/general_test.rb +69 -0
  73. data/test/inputs/grouped_collection_select_input_test.rb +109 -0
  74. data/test/inputs/hidden_input_test.rb +30 -0
  75. data/test/inputs/numeric_input_test.rb +167 -0
  76. data/test/inputs/priority_input_test.rb +43 -0
  77. data/test/inputs/readonly_test.rb +61 -0
  78. data/test/inputs/required_test.rb +113 -0
  79. data/test/inputs/string_input_test.rb +140 -0
  80. data/test/inputs/text_input_test.rb +24 -0
  81. data/test/{discovery_inputs.rb → support/discovery_inputs.rb} +0 -0
  82. data/test/support/misc_helpers.rb +48 -6
  83. data/test/support/mock_controller.rb +2 -2
  84. data/test/support/models.rb +20 -5
  85. data/test/test_helper.rb +5 -8
  86. metadata +123 -98
  87. data/.gitignore +0 -3
  88. data/.gitmodules +0 -3
  89. data/.travis.yml +0 -15
  90. data/CHANGELOG.rdoc +0 -159
  91. data/Gemfile +0 -9
  92. data/README.rdoc +0 -466
  93. data/Rakefile +0 -27
  94. data/lib/generators/simple_form/templates/config/initializers/simple_form.rb +0 -93
  95. data/lib/simple_form/components/wrapper.rb +0 -38
  96. data/lib/simple_form/helpers/has_errors.rb +0 -15
  97. data/lib/simple_form/helpers/maxlength.rb +0 -24
  98. data/lib/simple_form/helpers/pattern.rb +0 -28
  99. data/simple_form.gemspec +0 -25
  100. data/test/components/error_test.rb +0 -56
  101. data/test/components/hint_test.rb +0 -74
  102. data/test/components/wrapper_test.rb +0 -63
  103. data/test/custom_components.rb +0 -7
  104. data/test/form_builder_test.rb +0 -930
  105. data/test/inputs_test.rb +0 -995
data/.gitignore DELETED
@@ -1,3 +0,0 @@
1
- .bundle/
2
- pkg/
3
- Gemfile.lock
data/.gitmodules DELETED
@@ -1,3 +0,0 @@
1
- [submodule "test/support/country_select"]
2
- path = test/support/country_select
3
- url = git://github.com/rails/country_select
data/.travis.yml DELETED
@@ -1,15 +0,0 @@
1
- script: "git submodule update --init && bundle exec rake test"
2
- rvm:
3
- - 1.8.7
4
- - 1.9.2
5
- - 1.9.3
6
- - ruby-head
7
- - ree
8
- - jruby
9
- - rbx
10
- - rbx-2.0
11
- notifications:
12
- recipients:
13
- - jose.valim@plataformatec.com.br
14
- - carlos@plataformatec.com.br
15
- - rafael.franca@plataformatec.com.br
data/CHANGELOG.rdoc DELETED
@@ -1,159 +0,0 @@
1
- == 1.5.2
2
-
3
- * bug fix
4
- * Remove the internal usage of deprecated `:components`
5
-
6
- == 1.5.1
7
-
8
- * enhancements
9
- * `:components` options is now deprecated
10
-
11
- * bug fix
12
- * Fallback to default label when block is provided (github.com/pivotal-casebook)
13
- * Do not override default selection through attribute value in collection select when label/value methods are lambdas
14
-
15
- == 1.5.0
16
-
17
- * enhancements
18
- * Simplified generator by using directory action (by github.com/rupert654)
19
- * Support for `maxlength` on string inputs inferred from validation (by github.com/srbartlett)
20
- * Change form css class handling to only add the dom class when one is not given to the form call (by github.com/patrick99e99)
21
- * Support for required attributes when action validations are present (by github.com/csegura)
22
- * Don't generate `size` attribute for numeric input (by github.com/jasonmp85)
23
- * Support for `maxlength` on text area inputs inferred from validation
24
- * Support for `pattern` on text field inferred from validation when :pattern is true
25
- * Break Text, Password and File into their own inputs
26
- * Support easy enabling and disabling of components for specific inputs
27
- * Add HTML5 range input
28
-
29
- * bug fix
30
- * Fix bug when simple_fields_for is used with a hash like models and Rails 3.1
31
- * Fix bug that doesn't remove the item_wrapper_tag or the collection_wrapper_tag on collection inputs when nil or false value is passed to these options (by gitbub.com/dw2)
32
- * Fix bug that disable the entire select and wrapper when `disabled` option is a string or array
33
- * Fix bug when using label/value methods as procs together with disabled/selected options as procs for select inputs
34
-
35
- == 1.4.2
36
-
37
- * enhancements
38
- * Rails 3.1 support
39
-
40
- == 1.4.1
41
-
42
- * enhancements
43
- * ignore required attribute when conditional validations are present
44
-
45
- * bug fix
46
- * Do not use required='required' when browser validations are turned off
47
- * Sanitize HMTL attributes in error and hint helpers when options are present
48
- * Improve i18n lookup by ignoring explicit child index given to form builder (tests by github.com/rywall)
49
- * Fixes the form specific validation option if specified on the form itself (by github.com/medihack)
50
-
51
- == 1.4.0
52
-
53
- * enhancements
54
- * Add label class configuration option (by github.com/reu)
55
- * Improve i18n lookup (labels/hints/placeholders) for nested models
56
- * Use the given custom builder with simple_fields_for (by github.com/giniedp)
57
- * Add slim form generator (by github.com/fagiani)
58
- * Add form_class configuration option (by github.com/fagiani)
59
- * Default step of "any" for number input with non integer attributes (by github.com/fedesoria)
60
- * Add option to disable HTML5 browser validations on all forms (by github.com/coryschires)
61
- * Add option to disable all HTML5 extensions (by github.com/wolframarnold)
62
- * Add input_field helper to form builder (by github.com/jeroenhouben)
63
- * Allow inputs to be discovered on demand by placing them at app/inputs (a la formtastic)
64
- * Add full_error on that shows the error with the attribute name
65
-
66
- * bug fix
67
- * Fix for file attributes automatic detection, to work with virtual attributes
68
- * Fix for numeric fields validation options using symbols and procs
69
- * Fix password attributes to add size and maxlength options the same way as string (by github.com/fedesoria)
70
- * Fix bug with custom form builders and new mappings being added to the superclass builder (by github.com/rdvdijk)
71
- * Fix HTML validation issue with collection_check_boxes
72
-
73
- == 1.3.1
74
-
75
- * enhancements
76
- * Add :autofocus HTML5 attribute support (by github.com/jpzwarte)
77
- * Add possibility to specify custom builder and inherit mappings (by github.com/rejeep)
78
- * Make custom mappings work with all attributes types (by github.com/rafaelfranca)
79
- * Add support for procs/lambdas in text/value methods for collection_select
80
-
81
- * deprecation
82
- * removed the deprecated :remote_form_for
83
-
84
- * bug fix
85
- * Only add the "required" HTML 5 attribute for valid inputs, disable in selects (not allowed)
86
- * Fix error when using hints without an attribute (by github.com/butsjoh and github.com/rafaelfranca)
87
- * Fix messy html output for hint, error and label components (by github.com/butsjoh and github.com/rafaelfranca)
88
- * Allow direct setting of for attribute on label (by github.com/Bertg)
89
-
90
- == 1.3.0
91
-
92
- * enhancements
93
- * Allow collection input to accept a collection of symbols
94
- * Add default css class to button
95
- * Allow forms for objects that don't respond to the "errors" method
96
- * collection_check_boxes and collection_radio now wrap the input in the label
97
- * Automatic add min/max values for numeric attributes based on validations and step for integers - HTML5 (by github.com/dasch)
98
- * Add :placeholder option for string inputs, allowing customization through I18n - HTML5 (by github.com/jonathan)
99
- * Add :search and :tel input types, with :tel mapping automatically from attributes matching "phone" - HTML5
100
- * Add :required html attribute for required inputs - HTML5
101
- * Add optional :components option to input to control component rendering (by github.com/khoan)
102
- * Add SimpleForm.translate as an easy way to turn off SimpleForm internal translations
103
- * Add :disabled option for all inputs (by github.com/fabiob)
104
- * Add collection wrapper tag and item wrapper tag to wrap elements in collection helpers - radio / check boxes
105
- * Add SimpleForm.input_mappings to allow configuring custom mappings for inputs (by github.com/TMaYaD)
106
-
107
- * bug fix
108
- * Search for validations on both association and attribute
109
- * Use controller.action_name to lookup action only when available, to fix issue with Rspec views tests (by github.com/rafaelfranca)
110
-
111
- == 1.2.2
112
-
113
- * enhancements
114
- * Compatibility with Rails 3 RC
115
-
116
- == 1.2.1
117
-
118
- * enhancements
119
- * Added haml generator support (by github.com/grimen)
120
- * Added error_notification message to form builder
121
- * Added required by default as configuration option
122
- * Added label_input as component, allowing boolean to change its order (input appearing first than label)
123
- * Added error_method to tidy up how errors are exhibited
124
- * Added error class on wrappers (by github.com/jduff)
125
- * Changed numeric types to have type=number for HTML5
126
-
127
- == 1.2.0
128
-
129
- * deprecation
130
- * Changed simple_form_install generator to simple_form:install
131
-
132
- * enhancements
133
- * Added support to presence validation to check if attribute is required or not (by github.com/gcirne)
134
- * Added .input as class to wrapper tag
135
- * Added config options for hint and error tags (by github.com/tjogin)
136
-
137
- == 1.1.3
138
-
139
- * deprecation
140
- * removed :conditions, :order, :joins and :include support in f.association
141
-
142
- == 1.1.2
143
-
144
- * bug fix
145
- * Ensure type is set to "text" and not "string"
146
-
147
- == 1.1.1
148
-
149
- * bug fix
150
- * Fix some escaping issues
151
-
152
- == 1.1.0
153
-
154
- * enhancements
155
- * Rails 3 support with generators, templates and HTML 5
156
-
157
- == 1.0
158
-
159
- * First release
data/Gemfile DELETED
@@ -1,9 +0,0 @@
1
- source "http://rubygems.org"
2
-
3
- gemspec
4
-
5
- gem "rake", '0.9.3.beta1'
6
- gem "rdoc"
7
- gem "mocha"
8
- gem "tzinfo"
9
- gem "ruby-debug", :platform => :mri_18
data/README.rdoc DELETED
@@ -1,466 +0,0 @@
1
- == SimpleForm
2
-
3
- Forms made easy (for Rails)!
4
-
5
- SimpleForm aims to be as flexible as possible while helping you with powerful components to create your forms. The basic goal of simple form is to not touch your way of defining the layout, letting you find the better design for your eyes. Good part of the DSL was inherited from Formtastic, which we are thankful for and should make you feel right at home.
6
-
7
- == Information
8
-
9
- === Google Group
10
-
11
- If you have any questions, comments, or concerns please use the Google Group instead of the GitHub Issues tracker:
12
-
13
- http://groups.google.com/group/plataformatec-simpleform
14
-
15
- === RDocs
16
-
17
- You can view the SimpleForm documentation in RDoc format here:
18
-
19
- http://rubydoc.info/github/plataformatec/simple_form/master/frames
20
-
21
- If you need to use SimpleForm with Rails 2.3, you can always run `gem server` from the command line after you install the gem to access the old documentation.
22
-
23
- === Bug reports
24
-
25
- If you discover any bugs, feel free to create an issue on GitHub. Please add as much information as possible to help us fixing the possible bug. We also encourage you to help even more by forking and sending us a pull request.
26
-
27
- http://github.com/plataformatec/simple_form/issues
28
-
29
- == Installation
30
-
31
- Install the gem:
32
-
33
- gem install simple_form
34
-
35
- Add it to your Gemfile:
36
-
37
- gem "simple_form"
38
-
39
- Run the generator:
40
-
41
- rails generate simple_form:install
42
-
43
- Also, if you want to use the country select, you will need the country_select plugin, install with following command:
44
-
45
- rails plugin install git://github.com/rails/country_select.git
46
-
47
- And you are ready to go. Since this branch aims Rails 3 support,
48
- if you want to use it with Rails 2.3 you should check this branch:
49
-
50
- http://github.com/plataformatec/simple_form/tree/v1.0
51
-
52
- == Usage
53
-
54
- SimpleForm was designed to be customized as you need to. Basically it's a stack of components that are invoked to create a complete html input for you, which by default contains label, hints, errors and the input itself. It does not aim to create a lot of different logic from the default Rails form helpers, as they do a great work by themselves. Instead, SimpleForm acts as a DSL and just maps your input type (retrieved from the column definition in the database) to an specific helper method.
55
-
56
- To start using SimpleForm you just have to use the helper it provides:
57
-
58
- <%= simple_form_for @user do |f| %>
59
- <%= f.input :username %>
60
- <%= f.input :password %>
61
- <%= f.button :submit %>
62
- <% end %>
63
-
64
- This will generate an entire form with labels for user name and password as well, and render errors by default when you render the form with invalid data (after submitting for example).
65
-
66
- You can overwrite the default label by passing it to the input method, add a hint or even a placeholder:
67
-
68
- <%= simple_form_for @user do |f| %>
69
- <%= f.input :username, :label => 'Your username please' %>
70
- <%= f.input :password, :hint => 'No special characters.' %>
71
- <%= f.input :email, :placeholder => 'user@domain.com' %>
72
- <%= f.button :submit %>
73
- <% end %>
74
-
75
- You can also disable labels, hints or error or configure the html of any of them:
76
-
77
- <%= simple_form_for @user do |f| %>
78
- <%= f.input :username, :label_html => { :class => 'my_class' } %>
79
- <%= f.input :password, :hint => false, :error_html => { :id => "password_error"} %>
80
- <%= f.input :password_confirmation, :label => false %>
81
- <%= f.button :submit %>
82
- <% end %>
83
-
84
- It is also possible to pass any html attribute straight to the input, by using the :input_html option, for instance:
85
-
86
- <%= simple_form_for @user do |f| %>
87
- <%= f.input :username, :input_html => { :class => 'special' } %>
88
- <%= f.input :password, :input_html => { :maxlength => 20 } %>
89
- <%= f.input :remember_me, :input_html => { :value => '1' } %>
90
- <%= f.button :submit %>
91
- <% end %>
92
-
93
- By default all inputs are required, which means an * is prepended to the label, but you can disable it in any input you want:
94
-
95
- <%= simple_form_for @user do |f| %>
96
- <%= f.input :name, :required => false %>
97
- <%= f.input :username %>
98
- <%= f.input :password %>
99
- <%= f.button :submit %>
100
- <% end %>
101
-
102
- SimpleForm also lets you overwrite the default input type it creates:
103
-
104
- <%= simple_form_for @user do |f| %>
105
- <%= f.input :username %>
106
- <%= f.input :password %>
107
- <%= f.input :description, :as => :text %>
108
- <%= f.input :accepts, :as => :radio %>
109
- <%= f.button :submit %>
110
- <% end %>
111
-
112
- So instead of a checkbox for the :accepts attribute, you'll have a pair of radio buttons with yes/no labels and a text area instead of a text field for the description. You can also render boolean attributes using :as => :select to show a dropdown.
113
-
114
- It is also possible to give the :disabled option to SimpleForm, and it'll automatically mark the wrapper as disabled with a css class, so you can style labels, hints and other components inside the wrapper as well:
115
-
116
- <%= simple_form_for @user do |f| %>
117
- <%= f.input :username, :disabled => true, :hint => "You cannot change your username." %>
118
- <%= f.button :submit %>
119
- <% end %>
120
-
121
- SimpleForm accepts same options as their corresponding input type helper in Rails:
122
-
123
- <%= simple_form_for @user do |f| %>
124
- <%= f.input :date_of_birth, :as => :date, :start_year => Date.today.year - 90,
125
- :end_year => Date.today.year - 12, :discard_day => true,
126
- :order => [:month, :year] %>
127
- <%= f.button :submit %>
128
- <% end %>
129
-
130
- SimpleForm also allows you to use label, hint, input_field, error and full_error helpers it provides (please take a look at the rdocs for each method for more info):
131
-
132
- <%= simple_form_for @user do |f| %>
133
- <%= f.label :username %>
134
- <%= f.input_field :username %>
135
- <%= f.hint 'No special characters, please!' %>
136
- <%= f.error :username, :id => 'user_name_error' %>
137
- <%= f.full_error :token %>
138
- <%= f.submit 'Save' %>
139
- <% end %>
140
-
141
- Any extra option passed to these methods will be rendered as html option.
142
-
143
- === Collections
144
-
145
- And what if you want to create a select containing the age from 18 to 60 in your form? You can do it overriding the :collection option:
146
-
147
- <%= simple_form_for @user do |f| %>
148
- <%= f.input :user %>
149
- <%= f.input :age, :collection => 18..60 %>
150
- <%= f.button :submit %>
151
- <% end %>
152
-
153
- Collections can be arrays or ranges, and when a :collection is given the :select input will be rendered by default, so we don't need to pass the :as => :select option. Other types of collection are :radio and :check_boxes. Those are added by SimpleForm to Rails set of form helpers (read Extra Helpers session below for more information).
154
-
155
- Collection inputs accepts two other options beside collections:
156
-
157
- * label_method => the label method to be applied to the collection to retrieve the label
158
-
159
- * value_method => the value method to be applied to the collection to retrieve the value
160
-
161
- Those methods are useful to manipulate the given collection. Both of these options also accept lambda/procs in case you want to calculate the value or label in a special way eg. custom translation. All other options given are sent straight to the underlying helper. For example, you can give prompt as:
162
-
163
- f.input :age, :collection => 18..60, :prompt => "Select your age"
164
-
165
- === Priority
166
-
167
- SimpleForm also supports :time_zone and :country. When using such helpers, you can give :priority as option to select which time zones and/or countries should be given higher priority:
168
-
169
- f.input :residence_country, :priority => [ "Brazil" ]
170
- f.input :time_zone, :priority => /US/
171
-
172
- Those values can also be configured with a default value to be used site use through the SimpleForm.country_priority and SimpleForm.time_zone_priority helpers.
173
-
174
- === Wrapper
175
-
176
- SimpleForm allows you to add a wrapper which contains the label, error, hint and input.
177
- The first step is to configure a wrapper tag:
178
-
179
- SimpleForm.wrapper_tag = :p
180
-
181
- And now, you don't need to wrap your f.input calls anymore:
182
-
183
- <%= simple_form_for @user do |f| %>
184
- <%= f.input :username %>
185
- <%= f.input :password %>
186
- <%= f.button :submit %>
187
- <% end %>
188
-
189
- == Associations
190
-
191
- To deal with associations, SimpleForm can generate select inputs, a series of radios or check boxes. Lets see how it works: imagine you have a user model that belongs to a company and has_and_belongs_to_many roles. The structure would be something like:
192
-
193
- class User < ActiveRecord::Base
194
- belongs_to :company
195
- has_and_belongs_to_many :roles
196
- end
197
-
198
- class Company < ActiveRecord::Base
199
- has_many :users
200
- end
201
-
202
- class Role < ActiveRecord::Base
203
- has_and_belongs_to_many :users
204
- end
205
-
206
- Now we have the user form:
207
-
208
- <%= simple_form_for @user do |f| %>
209
- <%= f.input :name %>
210
- <%= f.association :company %>
211
- <%= f.association :roles %>
212
- <%= f.button :submit %>
213
- <% end %>
214
-
215
- Simple enough right? This is going to render a :select input for choosing the :company, and another :select input with :multiple option for the :roles. You can of course change it, to use radios and check boxes as well:
216
-
217
- f.association :company, :as => :radio
218
- f.association :roles, :as => :check_boxes
219
-
220
- The association helper just invokes input under the hood, so all options available to :select, :radio and :check_boxes are also available to association. Additionally, you can specify the collection by hand, all together with the prompt:
221
-
222
- f.association :company, :collection => Company.active.all(:order => 'name'), :prompt => "Choose a Company"
223
-
224
- == Buttons
225
-
226
- All web forms need buttons, right? SimpleForm wraps them in the DSL, acting like a proxy:
227
-
228
- <%= simple_form_for @user do |f| %>
229
- <%= f.input :name %>
230
- <%= f.button :submit %>
231
- <% end %>
232
-
233
- The above will simply call submit. You choose to use it or not, it's just a question of taste.
234
-
235
- == Wrapping Rails Form Helpers
236
-
237
- Say you wanted to use a rails form helper but still wrap it in SimpleForm goodness? You can, by calling input with a block like so:
238
-
239
- <%= f.input :role do %>
240
- <%= f.select :role, Role.all.map { |r| [r.name, r.id, { :class => r.company.id }] }, :include_blank => true %>
241
- <% end %>
242
-
243
- In the above example, we're taking advantage of Rails 3's select method that allows us to pass in a hash of additional attributes for each option.
244
-
245
- == Extra helpers
246
-
247
- SimpleForm also comes with some extra helpers you can use inside rails default forms without relying on simple_form_for helper. They are listed below.
248
-
249
- === Simple Fields For
250
-
251
- Wrapper to use simple form inside a default rails form
252
-
253
- form_for @user do |f|
254
- f.simple_fields_for :posts do |posts_form|
255
- # Here you have all simple_form methods available
256
- posts_form.input :title
257
- end
258
- end
259
-
260
- === Collection Radio
261
-
262
- Creates a collection of radio inputs with labels associated (same API as collection_select):
263
-
264
- form_for @user do |f|
265
- f.collection_radio :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
266
- end
267
-
268
- <input id="user_options_true" name="user[options]" type="radio" value="true" />
269
- <label class="collection_radio" for="user_options_true">Yes</label>
270
- <input id="user_options_false" name="user[options]" type="radio" value="false" />
271
- <label class="collection_radio" for="user_options_false">No</label>
272
-
273
- === Collection Check Box
274
-
275
- Creates a collection of check boxes with labels associated (same API as collection_select):
276
-
277
- form_for @user do |f|
278
- f.collection_check_boxes :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
279
- end
280
-
281
- <input name="user[options][]" type="hidden" value="" />
282
- <input id="user_options_true" name="user[options][]" type="checkbox" value="true" />
283
- <label class="collection_check_box" for="user_options_true">Yes</label>
284
- <input name="user[options][]" type="hidden" value="" />
285
- <input id="user_options_false" name="user[options][]" type="checkbox" value="false" />
286
- <label class="collection_check_box" for="user_options_false">No</label>
287
-
288
- To use this with associations in your model, you can do the following:
289
-
290
- form_for @user do |f|
291
- f.collection_check_boxes :role_ids, Role.all, :id, :name # using :roles here is not going to work.
292
- end
293
-
294
-
295
- == Mappings/Inputs available
296
-
297
- SimpleForm comes with a lot of default mappings:
298
-
299
- Mapping Input Column Type
300
-
301
- boolean check box boolean
302
- string text field string
303
- email email field string with name matching "email"
304
- url url field string with name matching "url"
305
- tel tel field string with name matching "phone"
306
- password password field string with name matching "password"
307
- search search -
308
- text text area text
309
- file file field string, responding to file methods
310
- hidden hidden field -
311
- integer number field integer
312
- float number field float
313
- decimal number field decimal
314
- range range field -
315
- datetime datetime select datetime/timestamp
316
- date date select date
317
- time time select time
318
- select collection select belongs_to/has_many/has_and_belongs_to_many associations
319
- radio collection radio belongs_to
320
- check_boxes collection check box has_many/has_and_belongs_to_many associations
321
- country country select string with name matching "country"
322
- time_zone time zone select string with name matching "time_zone"
323
-
324
- == Custom inputs
325
-
326
- It is very easy to add custom inputs to SimpleForm. For instance, if you want to add a custom input that extends the string one, you just need to add this file:
327
-
328
- # app/inputs/currency_input.rb
329
- class CurrencyInput < SimpleForm::Inputs::Base
330
- def input
331
- "$ #{@builder.text_field(attribute_name, input_html_options)}".html_safe
332
- end
333
- end
334
-
335
- And use it in your views:
336
-
337
- f.input :money, :as => :currency
338
-
339
- You can also redefine existing SimpleForm inputs by creating a new class with the same name. For instance, if you want to wrap date/time/datetime in a div, you can do:
340
-
341
- # app/inputs/date_time_input.rb
342
- class DateTimeInput < SimpleForm::Inputs::DateTimeInput
343
- def input
344
- "<div>#{super}</div>".html_safe
345
- end
346
- end
347
-
348
- == Custom form builder
349
-
350
- You can create a custom form builder that uses SimpleForm.
351
-
352
- Create a helper method that calls simple_form_for with a custom builder:
353
-
354
- def custom_form_for(object, *args, &block)
355
- options = args.extract_options!
356
- simple_form_for(object, *(args << options.merge(:builder => CustomFormBuilder)), &block)
357
- end
358
-
359
- Create a form builder class that inherits from SimpleForm::FormBuilder.
360
-
361
- class CustomFormBuilder < SimpleForm::FormBuilder
362
- def input(attribute_name, options = {}, &block)
363
- options[:input_html].merge! :class => 'custom'
364
- super
365
- end
366
- end
367
-
368
- == I18n
369
-
370
- SimpleForm uses all power of I18n API to lookup labels, hints and placeholders. To customize your forms you can create a locale file like this:
371
-
372
- en:
373
- simple_form:
374
- labels:
375
- user:
376
- username: 'User name'
377
- password: 'Password'
378
- hints:
379
- user:
380
- username: 'User name to sign in.'
381
- password: 'No special characters, please.'
382
- placeholders:
383
- user:
384
- username: 'Your username'
385
- password: '****'
386
-
387
- And your forms will use this information to render the components for you.
388
-
389
- SimpleForm also lets you be more specific, separating lookups through actions for labels, hints and placeholders. Let's say you want a different label for new and edit actions, the locale file would be something like:
390
-
391
- en:
392
- simple_form:
393
- labels:
394
- user:
395
- username: 'User name'
396
- password: 'Password'
397
- edit:
398
- username: 'Change user name'
399
- password: 'Change password'
400
-
401
- This way SimpleForm will figure out the right translation for you, based on the action being rendered. And to be a little bit DRYer with your locale file, you can skip the model information inside it:
402
-
403
- en:
404
- simple_form:
405
- labels:
406
- username: 'User name'
407
- password: 'Password'
408
- hints:
409
- username: 'User name to sign in.'
410
- password: 'No special characters, please.'
411
- placeholders:
412
- username: 'Your username'
413
- password: '****'
414
-
415
- SimpleForm will always look for a default attribute translation if no specific is found inside the model key. In addition, SimpleForm will fallback to default human_attribute_name from Rails when no other translation is found for labels.
416
-
417
- Finally, you can also overwrite any label, hint or placeholder inside your view, just by passing the option manually. This way the I18n lookup will be skipped.
418
-
419
- It's also possible to translate buttons, using Rails' built-in I18n support:
420
-
421
- en:
422
- helpers:
423
- submit:
424
- user:
425
- create: "Add %{model}"
426
- update: "Save Changes"
427
-
428
- There are other options that can be configured through I18n API, such as required text and boolean. Be sure to check our locale file or the one copied to your application after you run "rails generate simple_form:install".
429
-
430
- == HTML 5 Notice
431
-
432
- By default, SimpleForm will generate input field types and attributes that are supported in HTML5, but are considered invalid HTML for older document types such as HTML4 or XHTML1.0. The HTML5 extensions include the new field types such as email, number, search, url, tel, and the new attributes such as required, autofocus, maxlength, min, max, step.
433
-
434
- Most browsers will not care, but some of the newer ones - in particular Chrome 10+ - use the required attribute to force a value into an input and will prevent form submission without it. Depending on the design of the application this may or may not be desired. In many cases it can break existing UI's.
435
-
436
- It is possible to disable all HTML 5 extensions in SimpleForm with the following configuration:
437
-
438
- SimpleForm.html5 = false # default is true
439
-
440
- If you want to have all other HTML 5 features, such as the new field types, you can disable only the browser validation:
441
-
442
- SimpleForm.browser_validations = false # default is true
443
-
444
- This option adds a new `novalidate` property to the form, instructing it to skip all HTML 5 validation. The inputs will still be generated with the required and other attributes, that might help you to use some generic javascript validation.
445
-
446
- You can also add `novalidate` to a specific form by setting the option on the form itself:
447
-
448
- <%= simple_form_for(resource, :html => {:novalidate => true}) do |form| %>
449
-
450
- Please notice that any of the configurations above will disable the `placeholder` component, which is an HTML 5 feature. We believe most of the newest browsers are handling this attribute fine, and if they aren't, any plugin you use would take of using the placeholder attribute to do it. However, you can disable it if you want, by removing the placeholder component from the components list in SimpleForm configuration file.
451
-
452
- == Configuration
453
-
454
- SimpleForm has several configuration values. You can read and change them in the initializer created by SimpleForm, so if you haven't executed the command below yet, please do:
455
-
456
- rails generate simple_form:install
457
-
458
- == Maintainers
459
-
460
- * Carlos Antonio da Silva (https://github.com/carlosantoniodasilva)
461
- * Rafael Mendonça França (https://github.com/rafaelfranca)
462
-
463
- == License
464
-
465
- MIT License. Copyright 2011 Plataforma Tecnologia. http://blog.plataformatec.com.br
466
-