simple_form 1.2.0 → 1.4.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 (58) hide show
  1. data/.gitignore +2 -0
  2. data/.gitmodules +3 -0
  3. data/.travis.yml +6 -0
  4. data/CHANGELOG.rdoc +109 -0
  5. data/Gemfile +8 -0
  6. data/Gemfile.lock +82 -0
  7. data/MIT-LICENSE +20 -0
  8. data/README.rdoc +180 -53
  9. data/Rakefile +27 -0
  10. data/lib/generators/simple_form/USAGE +1 -1
  11. data/lib/generators/simple_form/install_generator.rb +5 -6
  12. data/lib/generators/simple_form/templates/_form.html.erb +2 -12
  13. data/lib/generators/simple_form/templates/_form.html.haml +10 -0
  14. data/lib/generators/simple_form/templates/_form.html.slim +10 -0
  15. data/lib/generators/simple_form/templates/en.yml +14 -0
  16. data/lib/generators/simple_form/templates/simple_form.rb +66 -12
  17. data/lib/simple_form/action_view_extensions/builder.rb +99 -40
  18. data/lib/simple_form/action_view_extensions/form_helper.rb +29 -6
  19. data/lib/simple_form/components/errors.rb +16 -6
  20. data/lib/simple_form/components/hints.rb +2 -2
  21. data/lib/simple_form/components/label_input.rb +13 -0
  22. data/lib/simple_form/components/labels.rb +5 -7
  23. data/lib/simple_form/components/placeholders.rb +22 -0
  24. data/lib/simple_form/components/wrapper.rb +19 -2
  25. data/lib/simple_form/components.rb +6 -4
  26. data/lib/simple_form/error_notification.rb +42 -0
  27. data/lib/simple_form/form_builder.rb +187 -72
  28. data/lib/simple_form/has_errors.rb +14 -0
  29. data/lib/simple_form/inputs/base.rb +106 -24
  30. data/lib/simple_form/inputs/block_input.rb +3 -2
  31. data/lib/simple_form/inputs/boolean_input.rb +22 -0
  32. data/lib/simple_form/inputs/collection_input.rb +46 -17
  33. data/lib/simple_form/inputs/date_time_input.rb +11 -5
  34. data/lib/simple_form/inputs/hidden_input.rb +11 -2
  35. data/lib/simple_form/inputs/mapping_input.rb +12 -6
  36. data/lib/simple_form/inputs/numeric_input.rb +55 -6
  37. data/lib/simple_form/inputs/priority_input.rb +5 -1
  38. data/lib/simple_form/inputs/string_input.rb +25 -11
  39. data/lib/simple_form/inputs.rb +1 -0
  40. data/lib/simple_form/map_type.rb +9 -6
  41. data/lib/simple_form/version.rb +1 -1
  42. data/lib/simple_form.rb +92 -8
  43. data/simple_form.gemspec +22 -0
  44. data/test/action_view_extensions/builder_test.rb +187 -51
  45. data/test/action_view_extensions/form_helper_test.rb +17 -5
  46. data/test/components/error_test.rb +23 -12
  47. data/test/components/hint_test.rb +4 -8
  48. data/test/components/label_test.rb +79 -11
  49. data/test/components/wrapper_test.rb +63 -0
  50. data/test/discovery_inputs.rb +21 -0
  51. data/test/error_notification_test.rb +62 -0
  52. data/test/form_builder_test.rb +332 -35
  53. data/test/inputs_test.rb +516 -53
  54. data/test/support/misc_helpers.rb +32 -4
  55. data/test/support/mock_controller.rb +4 -4
  56. data/test/support/models.rb +75 -11
  57. data/test/test_helper.rb +28 -12
  58. metadata +51 -11
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .bundle/
2
+ pkg/
data/.gitmodules ADDED
@@ -0,0 +1,3 @@
1
+ [submodule "test/support/country_select"]
2
+ path = test/support/country_select
3
+ url = git://github.com/rails/country_select
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ script: "git submodule update --init && rake test"
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - ree
6
+ - jruby
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,109 @@
1
+ == 1.4.0
2
+
3
+ * enhancements
4
+ * Add label class configuration option (by github.com/reu)
5
+ * Improve i18n lookup (labels/hints/placeholders) for nested models
6
+ * Use the given custom builder with simple_fields_for (by github.com/giniedp)
7
+ * Add slim form generator (by github.com/fagiani)
8
+ * Add form_class configuration option (by github.com/fagiani)
9
+ * Default step of "any" for number input with non integer attributes (by github.com/fedesoria)
10
+ * Add option to disable HTML5 browser validations on all forms (by github.com/coryschires)
11
+ * Add option to disable all HTML5 extensions (by github.com/wolframarnold)
12
+ * Add input_field helper to form builder (by github.com/jeroenhouben)
13
+ * Allow inputs to be discovered on demand by placing them at app/inputs (a la formtastic)
14
+ * Add full_error on that shows the error with the attribute name
15
+
16
+ * bug fix
17
+ * Fix for file attributes automatic detection, to work with virtual attributes
18
+ * Fix for numeric fields validation options using symbols and procs
19
+ * Fix password attributes to add size and maxlength options the same way as string (by github.com/fedesoria)
20
+ * Fix bug with custom form builders and new mappings being added to the superclass builder (by github.com/rdvdijk)
21
+ * Fix HTML validation issue with collection_check_boxes
22
+
23
+ == 1.3.1
24
+
25
+ * enhancements
26
+ * Add :autofocus HTML5 attribute support (by github.com/jpzwarte)
27
+ * Add possibility to specify custom builder and inherit mappings (by github.com/rejeep)
28
+ * Make custom mappings work with all attributes types (by github.com/rafaelfranca)
29
+ * Add support for procs/lambdas in text/value methods for collection_select
30
+
31
+ * deprecation
32
+ * removed the deprecated :remote_form_for
33
+
34
+ * bug fix
35
+ * Only add the "required" HTML 5 attribute for valid inputs, disable in selects (not allowed)
36
+ * Fix error when using hints without an attribute (by github.com/butsjoh and github.com/rafaelfranca)
37
+ * Fix messy html output for hint, error and label components (by github.com/butsjoh and github.com/rafaelfranca)
38
+ * Allow direct setting of for attribute on label (by github.com/Bertg)
39
+
40
+ == 1.3.0
41
+
42
+ * enhancements
43
+ * Allow collection input to accept a collection of symbols
44
+ * Add default css class to button
45
+ * Allow forms for objects that don't respond to the "errors" method
46
+ * collection_check_boxes and collection_radio now wrap the input in the label
47
+ * Automatic add min/max values for numeric attributes based on validations and step for integers - HTML5 (by github.com/dasch)
48
+ * Add :placeholder option for string inputs, allowing customization through I18n - HTML5 (by github.com/jonathan)
49
+ * Add :search and :tel input types, with :tel mapping automatically from attributes matching "phone" - HTML5
50
+ * Add :required html attribute for required inputs - HTML5
51
+ * Add optional :components option to input to control component rendering (by github.com/khoan)
52
+ * Add SimpleForm.translate as an easy way to turn off SimpleForm internal translations
53
+ * Add :disabled option for all inputs (by github.com/fabiob)
54
+ * Add collection wrapper tag and item wrapper tag to wrap elements in collection helpers - radio / check boxes
55
+ * Add SimpleForm.input_mappings to allow configuring custom mappings for inputs (by github.com/TMaYaD)
56
+
57
+ * bug fix
58
+ * Search for validations on both association and attribute
59
+ * Use controller.action_name to lookup action only when available, to fix issue with Rspec views tests (by github.com/rafaelfranca)
60
+
61
+ == 1.2.2
62
+
63
+ * enhancements
64
+ * Compatibility with Rails 3 RC
65
+
66
+ == 1.2.1
67
+
68
+ * enhancements
69
+ * Added haml generator support (by github.com/grimen)
70
+ * Added error_notification message to form builder
71
+ * Added required by default as configuration option
72
+ * Added label_input as component, allowing boolean to change its order (input appearing first than label)
73
+ * Added error_method to tidy up how errors are exhibited
74
+ * Added error class on wrappers (by github.com/jduff)
75
+ * Changed numeric types to have type=number for HTML5
76
+
77
+ == 1.2.0
78
+
79
+ * deprecation
80
+ * Changed simple_form_install generator to simple_form:install
81
+
82
+ * enhancements
83
+ * Added support to presence validation to check if attribute is required or not (by github.com/gcirne)
84
+ * Added .input as class to wrapper tag
85
+ * Added config options for hint and error tags (by github.com/tjogin)
86
+
87
+ == 1.1.3
88
+
89
+ * deprecation
90
+ * removed :conditions, :order, :joins and :include support in f.association
91
+
92
+ == 1.1.2
93
+
94
+ * bug fix
95
+ * Ensure type is set to "text" and not "string"
96
+
97
+ == 1.1.1
98
+
99
+ * bug fix
100
+ * Fix some escaping issues
101
+
102
+ == 1.1.0
103
+
104
+ * enhancements
105
+ * Rails 3 support with generators, templates and HTML 5
106
+
107
+ == 1.0
108
+
109
+ * First release
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "rails", "~> 3.0.7"
4
+
5
+ group :test do
6
+ gem "mocha", :require => false
7
+ gem "ruby-debug", :require => false, :platform => :ruby_18
8
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,82 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ abstract (1.0.0)
5
+ actionmailer (3.0.7)
6
+ actionpack (= 3.0.7)
7
+ mail (~> 2.2.15)
8
+ actionpack (3.0.7)
9
+ activemodel (= 3.0.7)
10
+ activesupport (= 3.0.7)
11
+ builder (~> 2.1.2)
12
+ erubis (~> 2.6.6)
13
+ i18n (~> 0.5.0)
14
+ rack (~> 1.2.1)
15
+ rack-mount (~> 0.6.14)
16
+ rack-test (~> 0.5.7)
17
+ tzinfo (~> 0.3.23)
18
+ activemodel (3.0.7)
19
+ activesupport (= 3.0.7)
20
+ builder (~> 2.1.2)
21
+ i18n (~> 0.5.0)
22
+ activerecord (3.0.7)
23
+ activemodel (= 3.0.7)
24
+ activesupport (= 3.0.7)
25
+ arel (~> 2.0.2)
26
+ tzinfo (~> 0.3.23)
27
+ activeresource (3.0.7)
28
+ activemodel (= 3.0.7)
29
+ activesupport (= 3.0.7)
30
+ activesupport (3.0.7)
31
+ arel (2.0.9)
32
+ builder (2.1.2)
33
+ columnize (0.3.2)
34
+ erubis (2.6.6)
35
+ abstract (>= 1.0.0)
36
+ i18n (0.5.0)
37
+ linecache (0.43)
38
+ mail (2.2.18)
39
+ activesupport (>= 2.3.6)
40
+ i18n (>= 0.4.0)
41
+ mime-types (~> 1.16)
42
+ treetop (~> 1.4.8)
43
+ mime-types (1.16)
44
+ mocha (0.9.10)
45
+ rake
46
+ polyglot (0.3.1)
47
+ rack (1.2.2)
48
+ rack-mount (0.6.14)
49
+ rack (>= 1.0.0)
50
+ rack-test (0.5.7)
51
+ rack (>= 1.0)
52
+ rails (3.0.7)
53
+ actionmailer (= 3.0.7)
54
+ actionpack (= 3.0.7)
55
+ activerecord (= 3.0.7)
56
+ activeresource (= 3.0.7)
57
+ activesupport (= 3.0.7)
58
+ bundler (~> 1.0)
59
+ railties (= 3.0.7)
60
+ railties (3.0.7)
61
+ actionpack (= 3.0.7)
62
+ activesupport (= 3.0.7)
63
+ rake (>= 0.8.7)
64
+ thor (~> 0.14.4)
65
+ rake (0.8.7)
66
+ ruby-debug (0.10.4)
67
+ columnize (>= 0.1)
68
+ ruby-debug-base (~> 0.10.4.0)
69
+ ruby-debug-base (0.10.4)
70
+ linecache (>= 0.3)
71
+ thor (0.14.6)
72
+ treetop (1.4.9)
73
+ polyglot (>= 0.3.1)
74
+ tzinfo (0.3.27)
75
+
76
+ PLATFORMS
77
+ ruby
78
+
79
+ DEPENDENCIES
80
+ mocha
81
+ rails (~> 3.0.7)
82
+ ruby-debug
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 PlataformaTec http://blog.plataformatec.com.br/
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.rdoc CHANGED
@@ -2,13 +2,35 @@
2
2
 
3
3
  Forms made easy (for Rails)!
4
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, this way letting you find how 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.
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
6
28
 
7
29
  == Installation
8
30
 
9
31
  Install the gem:
10
32
 
11
- sudo gem install simple_form
33
+ gem install simple_form
12
34
 
13
35
  Add it to your Gemfile:
14
36
 
@@ -18,7 +40,11 @@ Run the generator:
18
40
 
19
41
  rails generate simple_form:install
20
42
 
21
- And you are ready to go. Since this branch is aims Rails 3 support,
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,
22
48
  if you want to use it with Rails 2.3 you should check this branch:
23
49
 
24
50
  http://github.com/plataformatec/simple_form/tree/v1.0
@@ -29,73 +55,100 @@ SimpleForm was designed to be customized as you need to. Basically it's a stack
29
55
 
30
56
  To start using SimpleForm you just have to use the helper it provides:
31
57
 
32
- <% simple_form_for @user do |f| -%>
58
+ <%= simple_form_for @user do |f| %>
33
59
  <%= f.input :username %>
34
60
  <%= f.input :password %>
35
61
  <%= f.button :submit %>
36
- <% end -%>
62
+ <% end %>
37
63
 
38
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).
39
65
 
40
- You can overwrite the default label by passing it to the input method, or even add a hint:
66
+ You can overwrite the default label by passing it to the input method, add a hint or even a placeholder:
41
67
 
42
- <% simple_form_for @user do |f| -%>
68
+ <%= simple_form_for @user do |f| %>
43
69
  <%= f.input :username, :label => 'Your username please' %>
44
70
  <%= f.input :password, :hint => 'No special characters.' %>
71
+ <%= f.input :email, :placeholder => 'user@domain.com' %>
45
72
  <%= f.button :submit %>
46
- <% end -%>
73
+ <% end %>
47
74
 
48
75
  You can also disable labels, hints or error or configure the html of any of them:
49
76
 
50
- <% simple_form_for @user do |f| -%>
77
+ <%= simple_form_for @user do |f| %>
51
78
  <%= f.input :username, :label_html => { :class => 'my_class' } %>
52
79
  <%= f.input :password, :hint => false, :error_html => { :id => "password_error"} %>
53
80
  <%= f.input :password_confirmation, :label => false %>
54
81
  <%= f.button :submit %>
55
- <% end -%>
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 %>
56
92
 
57
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:
58
94
 
59
- <% simple_form_for @user do |f| -%>
95
+ <%= simple_form_for @user do |f| %>
60
96
  <%= f.input :name, :required => false %>
61
97
  <%= f.input :username %>
62
98
  <%= f.input :password %>
63
99
  <%= f.button :submit %>
64
- <% end -%>
100
+ <% end %>
65
101
 
66
102
  SimpleForm also lets you overwrite the default input type it creates:
67
103
 
68
- <% simple_form_for @user do |f| -%>
104
+ <%= simple_form_for @user do |f| %>
69
105
  <%= f.input :username %>
70
106
  <%= f.input :password %>
71
107
  <%= f.input :description, :as => :text %>
72
108
  <%= f.input :accepts, :as => :radio %>
73
109
  <%= f.button :submit %>
74
- <% end -%>
110
+ <% end %>
75
111
 
76
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.
77
113
 
78
- SimpleForm also allows you using label, hint and error helpers it provides:
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:
79
122
 
80
- <% simple_form_for @user do |f| -%>
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| %>
81
133
  <%= f.label :username %>
82
- <%= f.text_field :username %>
83
- <%= f.error :username, :id => 'user_name_error' %>
134
+ <%= f.input_field :username %>
84
135
  <%= f.hint 'No special characters, please!' %>
136
+ <%= f.error :username, :id => 'user_name_error' %>
137
+ <%= f.full_error :token %>
85
138
  <%= f.submit 'Save' %>
86
- <% end -%>
139
+ <% end %>
87
140
 
88
- Any extra option passed to label, hint or error will be rendered as html option.
141
+ Any extra option passed to these methods will be rendered as html option.
89
142
 
90
143
  === Collections
91
144
 
92
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:
93
146
 
94
- <% simple_form_for @user do |f| -%>
147
+ <%= simple_form_for @user do |f| %>
95
148
  <%= f.input :user %>
96
149
  <%= f.input :age, :collection => 18..60 %>
97
150
  <%= f.button :submit %>
98
- <% end -%>
151
+ <% end %>
99
152
 
100
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).
101
154
 
@@ -105,7 +158,7 @@ Collection inputs accepts two other options beside collections:
105
158
 
106
159
  * value_method => the value method to be applied to the collection to retrieve the value
107
160
 
108
- Those methods are useful to manipulate the given collection. All other options given are sent straight to the underlying helper. For example, you can give prompt as:
161
+ Those methods are useful to manipulate the given collection. Both of these options also except 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:
109
162
 
110
163
  f.input :age, :collection => 18..60, :prompt => "Select your age"
111
164
 
@@ -125,17 +178,17 @@ The first step is to configure a wrapper tag:
125
178
 
126
179
  SimpleForm.wrapper_tag = :p
127
180
 
128
- And now, you don't need to wrap your f.input calls with anymore:
181
+ And now, you don't need to wrap your f.input calls anymore:
129
182
 
130
- <% simple_form_for @user do |f| -%>
183
+ <%= simple_form_for @user do |f| %>
131
184
  <%= f.input :username %>
132
185
  <%= f.input :password %>
133
186
  <%= f.button :submit %>
134
- <% end -%>
187
+ <% end %>
135
188
 
136
189
  == Associations
137
190
 
138
- To deal with associations, SimpleForm can generate select inputs or a series of radios or check boxes. Lets see how it works: imagine you have the user model that belongs to a company and has_and_belongs_to_many roles. The structure should be something like:
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:
139
192
 
140
193
  class User < ActiveRecord::Base
141
194
  belongs_to :company
@@ -152,17 +205,17 @@ To deal with associations, SimpleForm can generate select inputs or a series of
152
205
 
153
206
  Now we have the user form:
154
207
 
155
- <% simple_form_for @user do |f| -%>
208
+ <%= simple_form_for @user do |f| %>
156
209
  <%= f.input :name %>
157
210
  <%= f.association :company %>
158
211
  <%= f.association :roles %>
159
212
  <%= f.button :submit %>
160
- <% end -%>
213
+ <% end %>
161
214
 
162
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:
163
216
 
164
217
  f.association :company, :as => :radio
165
- f.association :roles, :as => :check_boxes
218
+ f.association :roles, :as => :check_boxes
166
219
 
167
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:
168
221
 
@@ -172,10 +225,10 @@ The association helper just invokes input under the hood, so all options availab
172
225
 
173
226
  All web forms need buttons, right? SimpleForm wraps them in the DSL, acting like a proxy:
174
227
 
175
- <% simple_form_for @user do |f| -%>
228
+ <%= simple_form_for @user do |f| %>
176
229
  <%= f.input :name %>
177
230
  <%= f.button :submit %>
178
- <% end -%>
231
+ <% end %>
179
232
 
180
233
  The above will simply call submit. You choose to use it or not, it's just a question of taste.
181
234
 
@@ -212,7 +265,7 @@ Creates a collection of radio inputs with labels associated (same API as collect
212
265
  Creates a collection of check boxes with labels associated (same API as collection_select):
213
266
 
214
267
  form_for @user do |f|
215
- f.collection_check_box :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
268
+ f.collection_check_boxes :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
216
269
  end
217
270
 
218
271
  <input name="user[options][]" type="hidden" value="" />
@@ -232,13 +285,15 @@ SimpleForm comes with a lot of default mappings:
232
285
  string text field string
233
286
  email email field string with name matching "email"
234
287
  url url field string with name matching "url"
288
+ tel tel field string with name matching "phone"
235
289
  password password field string with name matching "password"
290
+ search search -
236
291
  text text area text
237
292
  file file field string, responding to file methods
238
293
  hidden hidden field -
239
- integer text field integer
240
- float text field float
241
- decimal text field decimal
294
+ integer number field integer
295
+ float number field float
296
+ decimal number field decimal
242
297
  datetime datetime select datetime/timestamp
243
298
  date date select date
244
299
  time time select time
@@ -248,9 +303,53 @@ SimpleForm comes with a lot of default mappings:
248
303
  country country select string with name matching "country"
249
304
  time_zone time zone select string with name matching "time_zone"
250
305
 
306
+ == Custom inputs
307
+
308
+ 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:
309
+
310
+ # app/inputs/currency_input.rb
311
+ class CurrencyInput < SimpleForm::Inputs::StringInput
312
+ def input
313
+ "$ #{super}".html_safe
314
+ end
315
+ end
316
+
317
+ And use it in your views:
318
+
319
+ f.input :money, :as => :currency
320
+
321
+ 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:
322
+
323
+ # app/inputs/date_time_input.rb
324
+ class DateTimeInput < SimpleForm::Inputs::DateTimeInput
325
+ def input
326
+ "<div>#{super}</div>".html_safe
327
+ end
328
+ end
329
+
330
+ == Custom form builder
331
+
332
+ You can create a custom form builder that uses SimpleForm.
333
+
334
+ Create a helper method that calls simple_form_for with a custom builder:
335
+
336
+ def custom_form_for(object, *args, &block)
337
+ options = args.extract_options!
338
+ simple_form_for(object, *(args << options.merge(:builder => CustomFormBuilder)), &block)
339
+ end
340
+
341
+ Create a form builder class that inherits from SimpleForm::FormBuilder.
342
+
343
+ class CustomFormBuilder < SimpleForm::FormBuilder
344
+ def input(attribute_name, options = {}, &block)
345
+ options[:input_html].merge! :class => 'custom'
346
+ super
347
+ end
348
+ end
349
+
251
350
  == I18n
252
351
 
253
- SimpleForm uses all power of I18n API to lookup labels and hints for you. To customize your forms you can create a locale file like this:
352
+ 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:
254
353
 
255
354
  en:
256
355
  simple_form:
@@ -262,10 +361,14 @@ SimpleForm uses all power of I18n API to lookup labels and hints for you. To cus
262
361
  user:
263
362
  username: 'User name to sign in.'
264
363
  password: 'No special characters, please.'
364
+ placeholders:
365
+ user:
366
+ username: 'Your username'
367
+ password: '****'
265
368
 
266
- And your forms will use this information to render labels and hints for you.
369
+ And your forms will use this information to render the components for you.
267
370
 
268
- SimpleForm also lets you be more specific, separating lookups through actions for both hint and label. Let's say you want a different label for new and edit actions, the locale file would be something like:
371
+ 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:
269
372
 
270
373
  en:
271
374
  simple_form:
@@ -287,12 +390,42 @@ This way SimpleForm will figure out the right translation for you, based on the
287
390
  hints:
288
391
  username: 'User name to sign in.'
289
392
  password: 'No special characters, please.'
393
+ placeholders:
394
+ username: 'Your username'
395
+ password: '****'
396
+
397
+ 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.
398
+
399
+ 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.
290
400
 
291
- 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.
401
+ It's also possible to translate buttons, using Rails' built-in I18n support:
292
402
 
293
- Finally, you can also overwrite labels and hints inside your view, just by passing the label/hint manually. This way the I18n lookup will be skipped.
403
+ en:
404
+ helpers:
405
+ submit:
406
+ user:
407
+ create: "Add %{model}"
408
+ update: "Save Changes"
409
+
410
+ 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".
411
+
412
+ == HTML 5 Notice
413
+
414
+ 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.
415
+
416
+ 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.
417
+
418
+ It is possible to disable all HTML 5 extensions in SimpleForm with the following configuration:
419
+
420
+ SimpleForm.html5 = false # default is true
294
421
 
295
- There are other options that can be configured through I18n API, such as required text, boolean and button texts. Be sure to check our locale file or the one copied to your application after you run "script/generate simple_form_install".
422
+ If you want to have all other HTML 5 features, such as the new field types, you can disable only the browser validation:
423
+
424
+ SimpleForm.browser_validations = false # default is true
425
+
426
+ 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.
427
+
428
+ 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.
296
429
 
297
430
  == Configuration
298
431
 
@@ -300,19 +433,13 @@ SimpleForm has several configuration values. You can read and change them in the
300
433
 
301
434
  rails generate simple_form:install
302
435
 
303
- == TODO
304
-
305
- Please refer to TODO file.
306
-
307
436
  == Maintainers
308
437
 
309
- * José Valim (http://github.com/josevalim)
310
- * Carlos Antonio da Silva (http://github.com/carlosantoniodasilva)
311
-
312
- == Bugs and Feedback
438
+ * José Valim (https://github.com/josevalim)
439
+ * Carlos Antonio da Silva (https://github.com/carlosantoniodasilva)
440
+ * Rafael Mendonça França (https://github.com/rafaelfranca)
313
441
 
314
- If you discover any bugs or want to drop a line, feel free to create an issue on GitHub.
442
+ == License
315
443
 
316
- http://github.com/plataformatec/simple_form/issues
444
+ MIT License. Copyright 2011 Plataforma Tecnologia. http://blog.plataformatec.com.br
317
445
 
318
- MIT License. Copyright 2010 Plataforma Tecnologia. http://blog.plataformatec.com.br
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'bundler'
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ require 'rake/testtask'
7
+ require 'rake/rdoctask'
8
+
9
+ desc 'Default: run unit tests.'
10
+ task :default => :test
11
+
12
+ desc 'Test the simple_form plugin.'
13
+ Rake::TestTask.new(:test) do |t|
14
+ t.libs << 'lib'
15
+ t.libs << 'test'
16
+ t.pattern = 'test/**/*_test.rb'
17
+ t.verbose = true
18
+ end
19
+
20
+ desc 'Generate documentation for the simple_form plugin.'
21
+ Rake::RDocTask.new(:rdoc) do |rdoc|
22
+ rdoc.rdoc_dir = 'rdoc'
23
+ rdoc.title = 'SimpleForm'
24
+ rdoc.options << '--line-numbers' << '--inline-source'
25
+ rdoc.rdoc_files.include('README.rdoc')
26
+ rdoc.rdoc_files.include('lib/**/*.rb')
27
+ end