formtastic 3.1.3 → 3.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/.travis.yml +12 -10
  2. data/Appraisals +11 -7
  3. data/CHANGELOG +12 -0
  4. data/DEPRECATIONS +3 -0
  5. data/{README.textile → README.md} +629 -616
  6. data/formtastic.gemspec +3 -3
  7. data/gemfiles/rails_3.2.gemfile +1 -0
  8. data/gemfiles/rails_edge.gemfile +5 -0
  9. data/lib/formtastic.rb +4 -0
  10. data/lib/formtastic/form_builder.rb +3 -0
  11. data/lib/formtastic/helpers.rb +1 -1
  12. data/lib/formtastic/helpers/enum.rb +13 -0
  13. data/lib/formtastic/helpers/fieldset_wrapper.rb +6 -6
  14. data/lib/formtastic/helpers/form_helper.rb +1 -1
  15. data/lib/formtastic/helpers/input_helper.rb +5 -1
  16. data/lib/formtastic/helpers/inputs_helper.rb +16 -20
  17. data/lib/formtastic/inputs/base/choices.rb +1 -1
  18. data/lib/formtastic/inputs/base/collections.rb +41 -4
  19. data/lib/formtastic/inputs/base/html.rb +7 -6
  20. data/lib/formtastic/inputs/base/naming.rb +4 -4
  21. data/lib/formtastic/inputs/base/options.rb +2 -3
  22. data/lib/formtastic/inputs/base/validations.rb +19 -3
  23. data/lib/formtastic/inputs/check_boxes_input.rb +10 -2
  24. data/lib/formtastic/inputs/country_input.rb +3 -1
  25. data/lib/formtastic/inputs/radio_input.rb +20 -0
  26. data/lib/formtastic/inputs/select_input.rb +28 -0
  27. data/lib/formtastic/inputs/time_zone_input.rb +16 -6
  28. data/lib/formtastic/localizer.rb +15 -15
  29. data/lib/formtastic/namespaced_class_finder.rb +1 -1
  30. data/lib/formtastic/version.rb +1 -1
  31. data/lib/generators/formtastic/form/form_generator.rb +1 -1
  32. data/lib/generators/formtastic/input/input_generator.rb +46 -0
  33. data/lib/generators/templates/formtastic.rb +10 -7
  34. data/lib/generators/templates/input.rb +19 -0
  35. data/spec/fast_spec_helper.rb +12 -0
  36. data/spec/generators/formtastic/input/input_generator_spec.rb +124 -0
  37. data/spec/helpers/form_helper_spec.rb +4 -4
  38. data/spec/inputs/base/collections_spec.rb +76 -0
  39. data/spec/inputs/base/validations_spec.rb +342 -0
  40. data/spec/inputs/check_boxes_input_spec.rb +66 -20
  41. data/spec/inputs/country_input_spec.rb +4 -4
  42. data/spec/inputs/radio_input_spec.rb +28 -0
  43. data/spec/inputs/readonly_spec.rb +50 -0
  44. data/spec/inputs/select_input_spec.rb +71 -11
  45. data/spec/inputs/time_zone_input_spec.rb +35 -9
  46. data/spec/spec_helper.rb +2 -30
  47. data/spec/support/shared_examples.rb +69 -0
  48. metadata +23 -12
  49. data/spec/support/deferred_garbage_collection.rb +0 -21
@@ -1,11 +1,12 @@
1
+ sudo: false
2
+ cache: bundler
1
3
  language: ruby
2
- before_install:
3
- - gem update --system
4
- - gem update bundler
5
4
  rvm:
6
5
  - 1.9.3
7
6
  - 2.0.0
8
- - 2.1
7
+ - 2.1.8
8
+ - 2.2.4
9
+ - 2.3.0
9
10
  gemfile:
10
11
  - gemfiles/rails_3.2.gemfile
11
12
  - gemfiles/rails_4.gemfile
@@ -14,16 +15,17 @@ gemfile:
14
15
  - gemfiles/rails_4.2.gemfile
15
16
  - gemfiles/rails_edge.gemfile
16
17
  env:
17
- - DEFER_GC=false RAILS_EDGE=true
18
+ before_install:
19
+ - gem install bundler --version 1.11.2
18
20
  script: "bundle exec rake spec"
19
21
  matrix:
20
- allow_failures:
22
+ exclude:
21
23
  - rvm: 1.9.3
22
24
  gemfile: gemfiles/rails_edge.gemfile
23
- env: DEFER_GC=false RAILS_EDGE=true
24
25
  - rvm: 2.0.0
25
26
  gemfile: gemfiles/rails_edge.gemfile
26
- env: DEFER_GC=false RAILS_EDGE=true
27
- - rvm: 2.1
27
+ - rvm: 2.1.8
28
28
  gemfile: gemfiles/rails_edge.gemfile
29
- env: DEFER_GC=false RAILS_EDGE=true
29
+ allow_failures:
30
+ - gemfile: gemfiles/rails_edge.gemfile
31
+ - rvm: 1.9.3
data/Appraisals CHANGED
@@ -1,5 +1,6 @@
1
1
  appraise 'rails-3.2' do
2
2
  gem 'rails', '~> 3.2.0'
3
+ gem 'test-unit-minitest', :platform => [:ruby_22, :ruby_23]
3
4
  end
4
5
 
5
6
  appraise 'rails-4' do
@@ -19,11 +20,14 @@ appraise 'rails-4.2' do
19
20
  gem 'rails', '~>4.2.0.beta4'
20
21
  end
21
22
 
22
- if ENV["RAILS_EDGE"] == "true"
23
- appraise 'rails-edge' do
24
- gem 'rails', :git => 'git://github.com/rails/rails.git'
25
- gem 'rack', :github => 'rack/rack'
26
- gem 'i18n', :github => 'svenfuchs/i18n'
27
- gem 'arel', :github => 'rails/arel'
28
- end
23
+ appraise 'rails-edge' do
24
+ gem 'rails', :git => 'git://github.com/rails/rails.git'
25
+ gem 'rack', :github => 'rack/rack'
26
+ gem 'i18n', :github => 'svenfuchs/i18n'
27
+ gem 'arel', :github => 'rails/arel'
28
+ gem 'rspec-rails', :github => 'rspec/rspec-rails'
29
+ gem 'rspec-mocks', :github => 'rspec/rspec-mocks'
30
+ gem 'rspec-support', :github => 'rspec/rspec-support'
31
+ gem 'rspec-core', :github => 'rspec/rspec-core'
32
+ gem 'rspec-expectations', :github => 'rspec/rspec-expectations'
29
33
  end
data/CHANGELOG CHANGED
@@ -1,3 +1,15 @@
1
+ 3.1.4
2
+ * Performance improvements (#1174)
3
+ * Added priority time zones configuration (#1172)
4
+ * Added input generators (#1150)
5
+ * Added support for `enum` (#1098)
6
+ * Added support for Reform gem by relying only on `defined_enums` Hash for enum collections (#1113)
7
+ * Added support for customizeable skipped columns (#1133)
8
+ * Added automatic input readonly attribute for attr_readonly attributes (#1158)
9
+ * Added support for multiple validation contexts (#1179)
10
+ * Rails 5 Beta support (#1183)
11
+ * Fixed typos and improved documentation
12
+
1
13
  3.1.3
2
14
 
3
15
  * Fixed (silenced) additional Rails 5 deprecation on column_for_attribute that we missed in 3.1.0
@@ -5,12 +5,15 @@ v4.0 (planned)
5
5
 
6
6
  * Remove support for Rails < 4.1
7
7
  * Remove support for country_select 1.x syntax (they want to remove it in 3.0)
8
+ * Remove support for input_class, custom_input_class_name, standard_input_class_name, action_class, custom_action_class_name, standard_action_class_name
8
9
 
9
10
  v3.1 (master)
10
11
 
11
12
  * Deprecate support for Rails < 4.1
12
13
  * Deprecate :member_value option
13
14
  * Deprecate :member_label option
15
+ * Deprecate internal custom input class finder methods (input_class, custom_input_class_name, standard_input_class_name)
16
+ * Deprecate internal custom action class finder methods (action_class, custom_action_class_name, standard_action_class_name)
14
17
 
15
18
  v3.0
16
19
 
@@ -1,616 +1,629 @@
1
- h1. Formtastic
2
-
3
- !https://travis-ci.org/justinfrench/formtastic.png?branch=master!:https://travis-ci.org/justinfrench/formtastic
4
- !http://inch-ci.org/github/justinfrench/formtastic.png!:http://inch-ci.org/github/justinfrench/formtastic
5
- !https://codeclimate.com/github/justinfrench/formtastic.png!:https://codeclimate.com/github/justinfrench/formtastic
6
- !https://badge.fury.io/rb/formtastic.png!:http://badge.fury.io/rb/formtastic
7
- !https://gemnasium.com/justinfrench/formtastic.png!:https://gemnasium.com/justinfrench/formtastic
8
-
9
- Formtastic is a Rails FormBuilder DSL (with some other goodies) to make it far easier to create beautiful, semantically rich, syntactically awesome, readily stylable and wonderfully accessible HTML forms in your Rails applications.
10
-
11
-
12
- h2. Documentation & Support
13
-
14
- * "Documentation is available on rdoc.info":http://rdoc.info/projects/justinfrench/formtastic
15
- * "We track issues & bugs on GitHub":http://github.com/justinfrench/formtastic/issues
16
- * "We have a wiki on GitHub":http://github.com/justinfrench/formtastic/wiki
17
- * "StackOverflow can help":http://stackoverflow.com/questions/tagged/formtastic
18
- * "Follow @formtastic on Twitter for news & updates":http://twitter.com/formtastic
19
-
20
-
21
- h2. Compatibility
22
-
23
- * Formtastic 4 will require Rails 4.1
24
- * Formtastic 3 requires Rails 3.2.13 minimum
25
- * Formtastic 2 requires Rails 3
26
- * Formtastic, much like Rails, is very ActiveRecord-centric. Many are successfully using other ActiveModel-like ORMs and objects (DataMapper, MongoMapper, Mongoid, Authlogic, Devise...) but we're not guaranteeing full compatibility at this stage. Patches are welcome!
27
-
28
-
29
- h2. The Story
30
-
31
- One day, I finally had enough, so I opened up my text editor, and wrote a DSL for how I'd like to author forms:
32
-
33
- <pre>
34
- <%= semantic_form_for @article do |f| %>
35
-
36
- <%= f.inputs :name => "Basic" do %>
37
- <%= f.input :title %>
38
- <%= f.input :body %>
39
- <%= f.input :section %>
40
- <%= f.input :publication_state, :as => :radio %>
41
- <%= f.input :category %>
42
- <%= f.input :allow_comments, :label => "Allow commenting on this article" %>
43
- <% end %>
44
-
45
- <%= f.inputs :name => "Advanced" do %>
46
- <%= f.input :keywords, :required => false, :hint => "Example: ruby, rails, forms" %>
47
- <%= f.input :extract, :required => false %>
48
- <%= f.input :description, :required => false %>
49
- <%= f.input :url_title, :required => false %>
50
- <% end %>
51
-
52
- <%= f.inputs :name => "Author", :for => :author do |author_form| %>
53
- <%= author_form.input :first_name %>
54
- <%= author_form.input :last_name %>
55
- <% end %>
56
-
57
- <%= f.actions do %>
58
- <%= f.action :submit, :as => :button %>
59
- <%= f.action :cancel, :as => :link %>
60
- <% end %>
61
-
62
- <% end %>
63
- </pre>
64
-
65
- I also wrote the accompanying HTML output I expected, favoring something very similar to the fieldsets, lists and other semantic elements Aaron Gustafson presented in "Learning to Love Forms":http://www.slideshare.net/AaronGustafson/learning-to-love-forms-web-directions-south-07, hacking together enough Ruby to prove it could be done.
66
-
67
-
68
- h2. It's awesome because...
69
-
70
- * It can handle @belongs_to@ associations (like Post belongs_to :author), rendering a select or set of radio inputs with choices from the parent model.
71
- * It can handle @has_many@ and @has_and_belongs_to_many@ associations (like: Post has_many :tags), rendering a multi-select with choices from the child models.
72
- * It's Rails 3/4 compatible (including nested forms).
73
- * It has internationalization (I18n)!
74
- * It's _really_ quick to get started with a basic form in place (4 lines), then go back to add in more detail if you need it.
75
- * There's heaps of elements, id and class attributes for you to hook in your CSS and JS.
76
- * It handles real world stuff like inline hints, inline error messages & help text.
77
- * It doesn't hijack or change any of the standard Rails form inputs, so you can still use them as expected (even mix and match).
78
- * It's got absolutely awesome spec coverage.
79
- * There's a bunch of people using and working on it (it's not just one developer building half a solution).
80
- * It has growing HTML5 support (new inputs like email/phone/search, new attributes like required/min/max/step/placeholder)
81
-
82
-
83
- h2. Opinions
84
-
85
- * It should be easier to do things the right way than the wrong way.
86
- * Sometimes _more mark-up_ is better.
87
- * Elements and attribute hooks are _gold_ for stylesheet authors.
88
- * Make the common things we do easy, yet ensure uncommon things are still possible.
89
-
90
-
91
- h2. Installation
92
-
93
- Simply add Formtastic to your Gemfile and bundle it up:
94
-
95
- <pre>
96
- gem 'formtastic', '~> 3.0'
97
- </pre>
98
-
99
- Run the installation generator:
100
-
101
- <pre lang=shell>
102
- $ rails generate formtastic:install
103
- </pre>
104
-
105
-
106
- h2. Stylesheets
107
-
108
- A proof-of-concept set of stylesheets are provided which you can include in your layout. Customization is best achieved by overriding these styles in an additional stylesheet.
109
-
110
- Rails 3.1 introduces an asset pipeline that allows plugins like Formtastic to serve their own Stylesheets, Javascripts, etc without having to run generators that copy them across to the host application. Formtastic makes three stylesheets available as an Engine, you just need to require them in your global stylesheets.
111
-
112
- <pre>
113
- # app/assets/stylesheets/application.css
114
- *= require formtastic
115
- *= require my_formtastic_changes
116
- </pre>
117
-
118
- Conditional stylesheets need to be compiled separately to prevent them being bundled and included with other application styles. Remove @require_tree .@ from application.css and specify required stylesheets individually.
119
-
120
- <pre>
121
- # app/assets/stylesheets/ie6.css
122
- *= require formtastic_ie6
123
-
124
- # app/assets/stylesheets/ie7.css
125
- *= require formtastic_ie7
126
- </pre>
127
-
128
- <pre>
129
- # app/views/layouts/application.html.erb
130
- <%= stylesheet_link_tag 'application' %>
131
- <!--[if IE 6]><%= stylesheet_link_tag 'ie6' %><![endif]-->
132
- <!--[if IE 7]><%= stylesheet_link_tag 'ie7' %><![endif]-->
133
- </pre>
134
-
135
- <pre>
136
- # config/environments/production.rb
137
- config.assets.precompile += %w( ie6.css ie7.css )
138
- </pre>
139
-
140
- h2. Usage
141
-
142
- Forms are really boring to code... you want to get onto the good stuff as fast as possible.
143
-
144
- This renders a set of inputs (one for _most_ columns in the database table, and one for each ActiveRecord @belongs_to@-association), followed by default action buttons (an input submit button):
145
-
146
- <pre>
147
- <%= semantic_form_for @user do |f| %>
148
- <%= f.inputs %>
149
- <%= f.actions %>
150
- <% end %>
151
- </pre>
152
-
153
- This is a great way to get something up fast, but like scaffolding, it's *not recommended for production*. Don't be so lazy!
154
-
155
- To specify the order of the fields, skip some of the fields or even add in fields that Formtastic couldn't infer. You can pass in a list of field names to @inputs@ and list of action names to @actions@:
156
-
157
- <pre>
158
- <%= semantic_form_for @user do |f| %>
159
- <%= f.inputs :title, :body, :section, :categories, :created_at %>
160
- <%= f.actions :submit, :cancel %>
161
- <% end %>
162
- </pre>
163
-
164
- You probably want control over the input type Formtastic uses for each field. You can expand the @inputs@ and @actions@ to block helper format and use the @:as@ option to specify an exact input type:
165
-
166
- <pre>
167
- <%= semantic_form_for @post do |f| %>
168
- <%= f.inputs do %>
169
- <%= f.input :title %>
170
- <%= f.input :body %>
171
- <%= f.input :section, :as => :radio %>
172
- <%= f.input :categories %>
173
- <%= f.input :created_at, :as => :string %>
174
- <% end %>
175
- <%= f.actions do %>
176
- <%= f.action :submit, :as => :button %>
177
- <%= f.action :cancel, :as => :link %>
178
- <% end %>
179
- <% end %>
180
- </pre>
181
-
182
- If you want to customize the label text, or render some hint text below the field, specify which fields are required/optional, or break the form into two fieldsets, the DSL is pretty comprehensive:
183
-
184
- <pre>
185
- <%= semantic_form_for @post do |f| %>
186
- <%= f.inputs "Basic", :id => "basic" do %>
187
- <%= f.input :title %>
188
- <%= f.input :body %>
189
- <% end %>
190
- <%= f.inputs :name => "Advanced Options", :id => "advanced" do %>
191
- <%= f.input :slug, :label => "URL Title", :hint => "Created automatically if left blank", :required => false %>
192
- <%= f.input :section, :as => :radio %>
193
- <%= f.input :user, :label => "Author" %>
194
- <%= f.input :categories, :required => false %>
195
- <%= f.input :created_at, :as => :string, :label => "Publication Date", :required => false %>
196
- <% end %>
197
- <%= f.actions do %>
198
- <%= f.action :submit %>
199
- <% end %>
200
- <% end %>
201
- </pre>
202
-
203
- You can create forms for nested resources:
204
-
205
- <pre>
206
- <%= semantic_form_for [@author, @post] do |f| %>
207
- </pre>
208
-
209
- Nested forms are also supported (don't forget your models need to be setup correctly with @accepts_nested_attributes_for@). You can do it in the Rails way:
210
-
211
- <pre>
212
- <%= semantic_form_for @post do |f| %>
213
- <%= f.inputs :title, :body, :created_at %>
214
- <%= f.semantic_fields_for :author do |author| %>
215
- <%= author.inputs :first_name, :last_name, :name => "Author" %>
216
- <% end %>
217
- <%= f.actions %>
218
- <% end %>
219
- </pre>
220
-
221
- Or the Formtastic way with the @:for@ option:
222
-
223
- <pre>
224
- <%= semantic_form_for @post do |f| %>
225
- <%= f.inputs :title, :body, :created_at %>
226
- <%= f.inputs :first_name, :last_name, :for => :author, :name => "Author" %>
227
- <%= f.actions %>
228
- <% end %>
229
- </pre>
230
-
231
- When working in has many association, you can even supply @"%i"@ in your fieldset name; they will be properly interpolated with the child index. For example:
232
-
233
- <pre>
234
- <%= semantic_form_for @post do |f| %>
235
- <%= f.inputs %>
236
- <%= f.inputs :name => 'Category #%i', :for => :categories %>
237
- <%= f.actions %>
238
- <% end %>
239
- </pre>
240
-
241
- Alternatively, the current index can be accessed via the `inputs` block's arguments for use anywhere:
242
-
243
- <pre>
244
- <%= semantic_form_for @post do |f| %>
245
- <%= f.inputs :for => :categories do |category, i| %>
246
- ...
247
- <%= f.actions %>
248
- <% end %>
249
- </pre>
250
-
251
- If you have more than one form on the same page, it may lead to HTML invalidation because of the way HTML element id attributes are assigned. You can provide a namespace for your form to ensure uniqueness of id attributes on form elements. The namespace attribute will be prefixed with underscore on the generate HTML id. For example:
252
-
253
- <pre>
254
- <%= semantic_form_for(@post, :namespace => 'cat_form') do |f| %>
255
- <%= f.inputs do %>
256
- <%= f.input :title %> # id="cat_form_post_title"
257
- <%= f.input :body %> # id="cat_form_post_body"
258
- <%= f.input :created_at %> # id="cat_form_post_created_at"
259
- <% end %>
260
- <%= f.actions %>
261
- <% end %>
262
- </pre>
263
-
264
- Customize HTML attributes for any input using the @:input_html@ option. Typically this is used to disable the input, change the size of a text field, change the rows in a textarea, or even to add a special class to an input to attach special behavior like "autogrow":http://plugins.jquery.com/project/autogrowtextarea textareas:
265
-
266
- <pre>
267
- <%= semantic_form_for @post do |f| %>
268
- <%= f.inputs do %>
269
- <%= f.input :title, :input_html => { :size => 10 } %>
270
- <%= f.input :body, :input_html => { :class => 'autogrow', :rows => 10, :cols => 20, :maxlength => 10 } %>
271
- <%= f.input :created_at, :input_html => { :disabled => true } %>
272
- <%= f.input :updated_at, :input_html => { :readonly => true } %>
273
- <% end %>
274
- <%= f.actions %>
275
- <% end %>
276
- </pre>
277
-
278
- The same can be done for actions with the @:button_html@ option:
279
-
280
- <pre>
281
- <%= semantic_form_for @post do |f| %>
282
- ...
283
- <%= f.actions do %>
284
- <%= f.action :submit, :button_html => { :class => "primary", :disable_with => 'Wait...' } %>
285
- <% end %>
286
- <% end %>
287
- </pre>
288
-
289
- Customize the HTML attributes for the @<li>@ wrapper around every input with the @:wrapper_html@ option hash. There's one special key in the hash: (@:class@), which will actually _append_ your string of classes to the existing classes provided by Formtastic (like @"required string error"@).
290
-
291
- <pre>
292
- <%= semantic_form_for @post do |f| %>
293
- <%= f.inputs do %>
294
- <%= f.input :title, :wrapper_html => { :class => "important" } %>
295
- <%= f.input :body %>
296
- <%= f.input :description, :wrapper_html => { :style => "display:none;" } %>
297
- <% end %>
298
- ...
299
- <% end %>
300
- </pre>
301
-
302
- Many inputs provide a collection of options to choose from (like @:select@, @:radio@, @:check_boxes@, @:boolean@). In many cases, Formtastic can find choices through the model associations, but if you want to use your own set of choices, the @:collection@ option is what you want. You can pass in an Array of objects, an array of Strings, a Hash... Throw almost anything at it! Examples:
303
-
304
- <pre>
305
- f.input :authors, :as => :check_boxes, :collection => User.order("last_name ASC").all
306
- f.input :authors, :as => :check_boxes, :collection => current_user.company.users.active
307
- f.input :authors, :as => :check_boxes, :collection => [@justin, @kate]
308
- f.input :authors, :as => :check_boxes, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
309
- f.input :author, :as => :select, :collection => Author.all
310
- f.input :author, :as => :select, :collection => Author.pluck(:first_name, :id)
311
- f.input :author, :as => :select, :collection => Author.pluck(Arel.sql("CONCAT(`first_name`, ' ', `last_name`)"), :id)
312
- f.input :author, :as => :select, :collection => Author.your_custom_scope_or_class_method
313
- f.input :author, :as => :select, :collection => { @justin.name => @justin.id, @kate.name => @kate.id }
314
- f.input :author, :as => :select, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
315
- f.input :author, :as => :radio, :collection => User.all
316
- f.input :author, :as => :radio, :collection => [@justin, @kate]
317
- f.input :author, :as => :radio, :collection => { @justin.name => @justin.id, @kate.name => @kate.id }
318
- f.input :author, :as => :radio, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
319
- f.input :admin, :as => :radio, :collection => ["Yes!", "No"]
320
- f.input :book_id, :as => :select, :collection => Hash[Book.all.map{|b| [b.name,b.id]}]
321
- f.input :fav_book,:as => :datalist , :collection => Book.pluck(:name)
322
- </pre>
323
-
324
-
325
- h2. The Available Inputs
326
-
327
- The Formtastic input types:
328
-
329
- * @:select@ - a select menu. Default for ActiveRecord associations: @belongs_to@, @has_many@, and @has_and_belongs_to_many@.
330
- * @:check_boxes@ - a set of check_box inputs. Alternative to @:select@ for ActiveRecord-associations: @has_many@, and @has_and_belongs_to_many@.
331
- * @:radio@ - a set of radio inputs. Alternative to @:select@ for ActiveRecord-associations: @belongs_to@.
332
- * @:time_zone@ - a select input. Default for column types: @:string@ with name matching @"time_zone"@.
333
- * @:password@ - a password input. Default for column types: @:string@ with name matching @"password"@.
334
- * @:text@ - a textarea. Default for column types: @:text@.
335
- * @:date_select@ - a date select. Default for column types: @:date@.
336
- * @:datetime_select@ - a date and time select. Default for column types: @:datetime@ and @:timestamp@.
337
- * @:time_select@ - a time select. Default for column types: @:time@.
338
- * @:boolean@ - a checkbox. Default for column types: @:boolean@.
339
- * @:string@ - a text field. Default for column types: @:string@.
340
- * @:number@ - a text field (just like string). Default for column types: @:integer@, @:float@, and @:decimal@.
341
- * @:file@ - a file field. Default for file-attachment attributes matching: "paperclip":http://github.com/thoughtbot/paperclip or "attachment_fu":http://github.com/technoweenie/attachment_fu.
342
- * @:country@ - a select menu of country names. Default for column types: :string with name @"country"@ - requires a *country_select* plugin to be installed.
343
- * @:email@ - a text field (just like string). Default for columns with name matching @"email"@. New in HTML5. Works on some mobile browsers already.
344
- * @:url@ - a text field (just like string). Default for columns with name matching @"url"@. New in HTML5. Works on some mobile browsers already.
345
- * @:phone@ - a text field (just like string). Default for columns with name matching @"phone"@ or @"fax"@. New in HTML5.
346
- * @:search@ - a text field (just like string). Default for columns with name matching @"search"@. New in HTML5. Works on Safari.
347
- * @:hidden@ - a hidden field. Creates a hidden field (added for compatibility).
348
- * @:range@ - a slider field.
349
- * @:datalist@ - a text field with a accompanying "datalist tag":https://developer.mozilla.org/en/docs/Web/HTML/Element/datalist which provides options for autocompletion
350
-
351
- The comments in the code are pretty good for each of these (what it does, what the output is, what the options are, etc.) so go check it out.
352
-
353
-
354
- h2. Delegation for label lookups
355
-
356
- Formtastic decides which label to use in the following order:
357
-
358
- <pre>
359
- 1. :label # :label => "Choose Title"
360
- 2. Formtastic i18n # if either :label => true || i18n_lookups_by_default = true (see Internationalization)
361
- 3. Activerecord i18n # if localization file found for the given attribute
362
- 4. label_str_method # if nothing provided this defaults to :humanize but can be set to a custom method
363
- </pre>
364
-
365
- h2. Internationalization (I18n)
366
-
367
- h3. Basic Localization
368
-
369
- Formtastic has some neat I18n-features. ActiveRecord object names and attributes are, by default, taken from calling @@object.human_name@ and @@object.human_attribute_name(attr)@ respectively. There are a few words specific to Formtastic that can be translated. See @lib/locale/en.yml@ for more information.
370
-
371
- Basic localization (labels only, with ActiveRecord):
372
-
373
- <pre>
374
- <%= semantic_form_for @post do |f| %>
375
- <%= f.inputs do %>
376
- <%= f.input :title %> # => :label => I18n.t('activerecord.attributes.user.title') or 'Title'
377
- <%= f.input :body %> # => :label => I18n.t('activerecord.attributes.user.body') or 'Body'
378
- <%= f.input :section %> # => :label => I18n.t('activerecord.attributes.user.section') or 'Section'
379
- <% end %>
380
- <% end %>
381
- </pre>
382
-
383
- *Note:* This is perfectly fine if you just want your labels/attributes and/or models to be translated using *ActiveRecord I18n attribute translations*, and you don't use input hints and legends. But what if you do? And what if you don't want same labels in all forms?
384
-
385
- h3. Enhanced Localization (Formtastic I18n API)
386
-
387
- Formtastic supports localized *labels*, *hints*, *legends*, *actions* using the I18n API for more advanced usage. Your forms can now be DRYer and more flexible than ever, and still fully localized. This is how:
388
-
389
- *1. Enable I18n lookups by default (@config/initializers/formtastic.rb@):*
390
-
391
- <pre>
392
- Formtastic::FormBuilder.i18n_lookups_by_default = true
393
- </pre>
394
-
395
- *2. Add some label-translations/variants (@config/locales/en.yml@):*
396
-
397
- <pre>
398
- en:
399
- formtastic:
400
- titles:
401
- post_details: "Post details"
402
- labels:
403
- post:
404
- title: "Your Title"
405
- body: "Write something..."
406
- edit:
407
- title: "Edit title"
408
- hints:
409
- post:
410
- title: "Choose a good title for your post."
411
- body: "Write something inspiring here."
412
- placeholders:
413
- post:
414
- title: "Title your post"
415
- slug: "Leave blank for an automatically generated slug"
416
- user:
417
- email: "you@yours.com"
418
- actions:
419
- create: "Create my %{model}"
420
- update: "Save changes"
421
- reset: "Reset form"
422
- cancel: "Cancel and go back"
423
- dummie: "Launch!"
424
- </pre>
425
-
426
- *3. ...and now you'll get:*
427
-
428
- <pre>
429
- <%= semantic_form_for Post.new do |f| %>
430
- <%= f.inputs do %>
431
- <%= f.input :title %> # => :label => "Choose a title...", :hint => "Choose a good title for your post."
432
- <%= f.input :body %> # => :label => "Write something...", :hint => "Write something inspiring here."
433
- <%= f.input :section %> # => :label => I18n.t('activerecord.attributes.user.section') or 'Section'
434
- <% end %>
435
- <%= f.actions do %>
436
- <%= f.action :submit %> # => "Create my %{model}"
437
- <% end %>
438
- <% end %>
439
- </pre>
440
-
441
- *4. Localized titles (a.k.a. legends):*
442
-
443
- _Note: Slightly different because Formtastic can't guess how you group fields in a form. Legend text can be set with first (as in the sample below) specified value, or :name/:title options - depending on what flavor is preferred._
444
-
445
- <pre>
446
- <%= semantic_form_for @post do |f| %>
447
- <%= f.inputs :post_details do %> # => :title => "Post details"
448
- # ...
449
- <% end %>
450
- # ...
451
- <% end %>
452
- </pre>
453
-
454
- *5. Override I18n settings:*
455
-
456
- <pre>
457
- <%= semantic_form_for @post do |f| %>
458
- <%= f.inputs do %>
459
- <%= f.input :title %> # => :label => "Choose a title...", :hint => "Choose a good title for your post."
460
- <%= f.input :body, :hint => false %> # => :label => "Write something..."
461
- <%= f.input :section, :label => 'Some section' %> # => :label => 'Some section'
462
- <% end %>
463
- <%= f.actions do %>
464
- <%= f.action :submit, :label => :dummie %> # => "Launch!"
465
- <% end %>
466
- <% end %>
467
- </pre>
468
-
469
- If I18n-lookups is disabled, i.e.:
470
-
471
- <pre>
472
- Formtastic::FormBuilder.i18n_lookups_by_default = false
473
- </pre>
474
-
475
- ...then you can enable I18n within the forms instead:
476
-
477
- <pre>
478
- <%= semantic_form_for @post do |f| %>
479
- <%= f.inputs do %>
480
- <%= f.input :title, :label => true %> # => :label => "Choose a title..."
481
- <%= f.input :body, :label => true %> # => :label => "Write something..."
482
- <%= f.input :section, :label => true %> # => :label => I18n.t('activerecord.attributes.user.section') or 'Section'
483
- <% end %>
484
- <%= f.actions do %>
485
- <%= f.action :submit, :label => true %> # => "Update %{model}" (if we are in edit that is...)
486
- <% end %>
487
- <% end %>
488
- </pre>
489
-
490
- *6. Advanced I18n lookups*
491
-
492
- For more flexible forms; Formtastic finds translations using a bottom-up approach taking the following variables in account:
493
-
494
- * @MODEL@, e.g. "post"
495
- * @ACTION@, e.g. "edit"
496
- * @KEY/ATTRIBUTE@, e.g. "title", :my_custom_key, ...
497
-
498
- ...in the following order:
499
-
500
- 1. @formtastic.{titles,labels,hints,actions}.MODEL.ACTION.ATTRIBUTE@ - by model and action
501
- 2. @formtastic.{titles,labels,hints,actions}.MODEL.ATTRIBUTE@ - by model
502
- 3. @formtastic.{titles,labels,hints,actions}.ATTRIBUTE@ - global default
503
-
504
- ...which means that you can define translations like this:
505
-
506
- <pre>
507
- en:
508
- formtastic:
509
- labels:
510
- title: "Title" # Default global value
511
- article:
512
- body: "Article content"
513
- post:
514
- new:
515
- title: "Choose a title..."
516
- body: "Write something..."
517
- edit:
518
- title: "Edit title"
519
- body: "Edit body"
520
- </pre>
521
-
522
- Values for @labels@/@hints@/@actions@ are can take values: @String@ (explicit value), @Symbol@ (i18n-lookup-key relative to the current "type", e.g. actions:), @true@ (force I18n lookup), @false@ (force no I18n lookup). Titles (legends) can only take: @String@ and @Symbol@ - true/false have no meaning.
523
-
524
- *7. Basic Translations*
525
- If you want some basic translations, take a look on the "formtastic_i18n gem":https://github.com/timoschilling/formtastic_i18n.
526
-
527
- h2. Semantic errors
528
-
529
- You can show errors on base (by default) and any other attribute just by passing its name to the semantic_errors method:
530
-
531
- <pre>
532
- <%= semantic_form_for @post do |f| %>
533
- <%= f.semantic_errors :state %>
534
- <% end %>
535
- </pre>
536
-
537
-
538
- h2. Modified & Custom Inputs
539
-
540
- You can modify existing inputs, subclass them, or create your own from scratch. Here's the basic process:
541
-
542
- * Create a file in @app/inputs@ with a filename ending in @_input.rb@. For example, @app/inputs/hat_size_input.rb@. Formtastic will automatically look in @app/inputs@ and find the file.
543
- * In that file, declare a classname ending in @Input@. For example, @class HatSizeInput@. It must have a @to_html@ method for rendering.
544
- * To use that input, leave off the word "input" in your @as@ statement. For example, @f.input(:size, :as => :hat_size)@
545
-
546
- Specific examples follow.
547
-
548
- h3. Changing Existing Input Behavior
549
-
550
- To modify the behavior of @StringInput@, subclass it in a new file, @app/inputs/string_input.rb@:
551
-
552
- <pre>
553
- class StringInput < Formtastic::Inputs::StringInput
554
- def to_html
555
- puts "this is my modified version of StringInput"
556
- super
557
- end
558
- end
559
- </pre>
560
-
561
- You can use your modified version with @:as => :string@.
562
-
563
- h3. Creating New Inputs Based on Existing Ones
564
-
565
- To create your own new types of inputs based on existing inputs, the process is similar. For example, to create @FlexibleTextInput@ based on @StringInput@, put the following in @app/inputs/flexible_text_input.rb@:
566
-
567
- <pre>
568
- class FlexibleTextInput < Formtastic::Inputs::StringInput
569
- def input_html_options
570
- super.merge(:class => "flexible-text-area")
571
- end
572
- end
573
- </pre>
574
-
575
- You can use your new input with @:as => :flexible_text@.
576
-
577
- h3. Creating New Inputs From Scratch
578
-
579
- To create a custom @DatePickerInput@ from scratch, put the following in @app/inputs/date_picker_input.rb@:
580
-
581
- <pre>
582
- class DatePickerInput
583
- include Formtastic::Inputs::Base
584
- def to_html
585
- # ...
586
- end
587
- end
588
- </pre>
589
-
590
- You can use your new input with @:as => :date_picker@.
591
-
592
-
593
- h2. Dependencies
594
-
595
- There are none other than Rails itself, but...
596
-
597
- * If you want to use the @:country@ input, you'll need to install the "country-select plugin":https://github.com/stefanpenner/country_select (or any other country_select plugin with the same API). Both 1.x and 2.x are supported, but they behave differently when storing data, so please see their "upgrade notes":https://github.com/stefanpenner/country_select/blob/master/UPGRADING.md if switching from 1.x.
598
- * There are a bunch of development dependencies if you plan to contribute to Formtastic
599
-
600
-
601
- h2. How to contribute
602
-
603
- * Fork the project on Github
604
- * Create a topic branch for your changes
605
- * Ensure that you provide *documentation* and *test coverage* for your changes (patches won't be accepted without)
606
- * Ensure that all tests pass (`bundle exec rake`)
607
- * Create a pull request on Github (these are also a great place to start a conversation around a patch as early as possible)
608
-
609
-
610
- h2. Project Info
611
-
612
- Formtastic was created by "Justin French":http://www.justinfrench.com with contributions from around 180 awesome developers. Run @git shortlog -n -s@ to see the awesome.
613
-
614
- The project is hosted on Github: "http://github.com/justinfrench/formtastic":http://github.com/justinfrench/formtastic, where your contributions, forkings, comments, issues and feedback are greatly welcomed.
615
-
616
- Copyright (c) 2007-2014 Justin French, released under the MIT license.
1
+ # Formtastic
2
+
3
+ [![Build Status](https://travis-ci.org/justinfrench/formtastic.svg?branch=master)](https://travis-ci.org/justinfrench/formtastic)
4
+ [![Inline docs](http://inch-ci.org/github/justinfrench/formtastic.svg?branch=master)](http://inch-ci.org/github/justinfrench/formtastic)
5
+ [![Code Climate](https://codeclimate.com/github/justinfrench/formtastic/badges/gpa.svg)](https://codeclimate.com/github/justinfrench/formtastic)
6
+ [![Gem Version](https://badge.fury.io/rb/formtastic.svg)](https://badge.fury.io/rb/formtastic)
7
+ [![Dependency](https://gemnasium.com/justinfrench/formtastic.png)](https://gemnasium.com/justinfrench/formtastic)
8
+
9
+ Formtastic is a Rails FormBuilder DSL (with some other goodies) to make it far easier to create beautiful, semantically rich, syntactically awesome, readily stylable and wonderfully accessible HTML forms in your Rails applications.
10
+
11
+ ## Documentation & Support
12
+
13
+ * [Documentation is available on rdoc.info](http://rdoc.info/projects/justinfrench/formtastic)
14
+ * [We track issues & bugs on GitHub](http://github.com/justinfrench/formtastic/issues)
15
+ * [We have a wiki on GitHub](http://github.com/justinfrench/formtastic/wiki)
16
+ * [StackOverflow can help](http://stackoverflow.com/questions/tagged/formtastic)
17
+ * [Follow @formtastic on Twitter for news & updates](http://twitter.com/formtastic)
18
+
19
+ ## Compatibility
20
+
21
+ * Formtastic 4 will require Rails 4.1
22
+ * Formtastic 3 requires Rails 3.2.13 minimum
23
+ * Formtastic 2 requires Rails 3
24
+ * Formtastic, much like Rails, is very ActiveRecord-centric. Many are successfully using other ActiveModel-like ORMs and objects (DataMapper, MongoMapper, Mongoid, Authlogic, Devise...) but we're not guaranteeing full compatibility at this stage. Patches are welcome!
25
+
26
+ ## The Story
27
+
28
+ One day, I finally had enough, so I opened up my text editor, and wrote a DSL for how I'd like to author forms:
29
+
30
+ ```erb
31
+ <%= semantic_form_for @article do |f| %>
32
+
33
+ <%= f.inputs :name => "Basic" do %>
34
+ <%= f.input :title %>
35
+ <%= f.input :body %>
36
+ <%= f.input :section %>
37
+ <%= f.input :publication_state, :as => :radio %>
38
+ <%= f.input :category %>
39
+ <%= f.input :allow_comments, :label => "Allow commenting on this article" %>
40
+ <% end %>
41
+
42
+ <%= f.inputs :name => "Advanced" do %>
43
+ <%= f.input :keywords, :required => false, :hint => "Example: ruby, rails, forms" %>
44
+ <%= f.input :extract, :required => false %>
45
+ <%= f.input :description, :required => false %>
46
+ <%= f.input :url_title, :required => false %>
47
+ <% end %>
48
+
49
+ <%= f.inputs :name => "Author", :for => :author do |author_form| %>
50
+ <%= author_form.input :first_name %>
51
+ <%= author_form.input :last_name %>
52
+ <% end %>
53
+
54
+ <%= f.actions do %>
55
+ <%= f.action :submit, :as => :button %>
56
+ <%= f.action :cancel, :as => :link %>
57
+ <% end %>
58
+
59
+ <% end %>
60
+ ```
61
+
62
+ I also wrote the accompanying HTML output I expected, favoring something very similar to the fieldsets, lists and other semantic elements Aaron Gustafson presented in [Learning to Love Forms](http://www.slideshare.net/AaronGustafson/learning-to-love-forms-web-directions-south-07), hacking together enough Ruby to prove it could be done.
63
+
64
+
65
+ ## It's awesome because...
66
+
67
+ * It can handle `belongs_to` associations (like Post belongs_to :author), rendering a select or set of radio inputs with choices from the parent model.
68
+ * It can handle `has_many` and `has_and_belongs_to_many` associations (like: Post has_many :tags), rendering a multi-select with choices from the child models.
69
+ * It's Rails 3/4 compatible (including nested forms).
70
+ * It has internationalization (I18n)!
71
+ * It's _really_ quick to get started with a basic form in place (4 lines), then go back to add in more detail if you need it.
72
+ * There's heaps of elements, id and class attributes for you to hook in your CSS and JS.
73
+ * It handles real world stuff like inline hints, inline error messages & help text.
74
+ * It doesn't hijack or change any of the standard Rails form inputs, so you can still use them as expected (even mix and match).
75
+ * It's got absolutely awesome spec coverage.
76
+ * There's a bunch of people using and working on it (it's not just one developer building half a solution).
77
+ * It has growing HTML5 support (new inputs like email/phone/search, new attributes like required/min/max/step/placeholder)
78
+
79
+
80
+ ## Opinions
81
+
82
+ * It should be easier to do things the right way than the wrong way.
83
+ * Sometimes _more mark-up_ is better.
84
+ * Elements and attribute hooks are _gold_ for stylesheet authors.
85
+ * Make the common things we do easy, yet ensure uncommon things are still possible.
86
+
87
+
88
+ ## Installation
89
+
90
+ Simply add Formtastic to your Gemfile and bundle it up:
91
+
92
+ ```ruby
93
+ gem 'formtastic', '~> 3.0'
94
+ ```
95
+
96
+ Run the installation generator:
97
+
98
+ ```shell
99
+ $ rails generate formtastic:install
100
+ ```
101
+
102
+
103
+ ## Stylesheets
104
+
105
+ A proof-of-concept set of stylesheets are provided which you can include in your layout. Customization is best achieved by overriding these styles in an additional stylesheet.
106
+
107
+ Rails 3.1 introduces an asset pipeline that allows plugins like Formtastic to serve their own Stylesheets, Javascripts, etc without having to run generators that copy them across to the host application. Formtastic makes three stylesheets available as an Engine, you just need to require them in your global stylesheets.
108
+
109
+ ```css
110
+ # app/assets/stylesheets/application.css
111
+ *= require formtastic
112
+ *= require my_formtastic_changes
113
+ ```
114
+
115
+ Conditional stylesheets need to be compiled separately to prevent them being bundled and included with other application styles. Remove `require_tree .` from application.css and specify required stylesheets individually.
116
+
117
+ ```css
118
+ # app/assets/stylesheets/ie6.css
119
+ *= require formtastic_ie6
120
+
121
+ # app/assets/stylesheets/ie7.css
122
+ *= require formtastic_ie7
123
+ ```
124
+
125
+ ```erb
126
+ # app/views/layouts/application.html.erb
127
+ <%= stylesheet_link_tag 'application' %>
128
+ <!--[if IE 6]><%= stylesheet_link_tag 'ie6' %><![endif]-->
129
+ <!--[if IE 7]><%= stylesheet_link_tag 'ie7' %><![endif]-->
130
+ ```
131
+
132
+ ```ruby
133
+ # config/environments/production.rb
134
+ config.assets.precompile += %w( ie6.css ie7.css )
135
+ ```
136
+
137
+ ## Usage
138
+
139
+ Forms are really boring to code... you want to get onto the good stuff as fast as possible.
140
+
141
+ This renders a set of inputs (one for _most_ columns in the database table, and one for each ActiveRecord `belongs_to`-association), followed by default action buttons (an input submit button):
142
+
143
+ ```erb
144
+ <%= semantic_form_for @user do |f| %>
145
+ <%= f.inputs %>
146
+ <%= f.actions %>
147
+ <% end %>
148
+ ```
149
+
150
+ This is a great way to get something up fast, but like scaffolding, it's *not recommended for production*. Don't be so lazy!
151
+
152
+ To specify the order of the fields, skip some of the fields or even add in fields that Formtastic couldn't infer. You can pass in a list of field names to `inputs` and list of action names to `actions`:
153
+
154
+ ```erb
155
+ <%= semantic_form_for @user do |f| %>
156
+ <%= f.inputs :title, :body, :section, :categories, :created_at %>
157
+ <%= f.actions :submit, :cancel %>
158
+ <% end %>
159
+ ```
160
+
161
+ You probably want control over the input type Formtastic uses for each field. You can expand the `inputs` and `actions` to block helper format and use the `:as` option to specify an exact input type:
162
+
163
+ ```erb
164
+ <%= semantic_form_for @post do |f| %>
165
+ <%= f.inputs do %>
166
+ <%= f.input :title %>
167
+ <%= f.input :body %>
168
+ <%= f.input :section, :as => :radio %>
169
+ <%= f.input :categories %>
170
+ <%= f.input :created_at, :as => :string %>
171
+ <% end %>
172
+ <%= f.actions do %>
173
+ <%= f.action :submit, :as => :button %>
174
+ <%= f.action :cancel, :as => :link %>
175
+ <% end %>
176
+ <% end %>
177
+ ```
178
+
179
+ If you want to customize the label text, or render some hint text below the field, specify which fields are required/optional, or break the form into two fieldsets, the DSL is pretty comprehensive:
180
+
181
+ ```erb
182
+ <%= semantic_form_for @post do |f| %>
183
+ <%= f.inputs "Basic", :id => "basic" do %>
184
+ <%= f.input :title %>
185
+ <%= f.input :body %>
186
+ <% end %>
187
+ <%= f.inputs :name => "Advanced Options", :id => "advanced" do %>
188
+ <%= f.input :slug, :label => "URL Title", :hint => "Created automatically if left blank", :required => false %>
189
+ <%= f.input :section, :as => :radio %>
190
+ <%= f.input :user, :label => "Author" %>
191
+ <%= f.input :categories, :required => false %>
192
+ <%= f.input :created_at, :as => :string, :label => "Publication Date", :required => false %>
193
+ <% end %>
194
+ <%= f.actions do %>
195
+ <%= f.action :submit %>
196
+ <% end %>
197
+ <% end %>
198
+ ```
199
+
200
+ You can create forms for nested resources:
201
+
202
+ ```erb
203
+ <%= semantic_form_for [@author, @post] do |f| %>
204
+ ```
205
+
206
+ Nested forms are also supported (don't forget your models need to be setup correctly with `accepts_nested_attributes_for`). You can do it in the Rails way:
207
+
208
+ ```erb
209
+ <%= semantic_form_for @post do |f| %>
210
+ <%= f.inputs :title, :body, :created_at %>
211
+ <%= f.semantic_fields_for :author do |author| %>
212
+ <%= author.inputs :first_name, :last_name, :name => "Author" %>
213
+ <% end %>
214
+ <%= f.actions %>
215
+ <% end %>
216
+ ```
217
+
218
+ Or the Formtastic way with the `:for` option:
219
+
220
+ ```erb
221
+ <%= semantic_form_for @post do |f| %>
222
+ <%= f.inputs :title, :body, :created_at %>
223
+ <%= f.inputs :first_name, :last_name, :for => :author, :name => "Author" %>
224
+ <%= f.actions %>
225
+ <% end %>
226
+ ```
227
+
228
+ When working in has many association, you can even supply `"%i"` in your fieldset name; they will be properly interpolated with the child index. For example:
229
+
230
+ ```erb
231
+ <%= semantic_form_for @post do |f| %>
232
+ <%= f.inputs %>
233
+ <%= f.inputs :name => 'Category #%i', :for => :categories %>
234
+ <%= f.actions %>
235
+ <% end %>
236
+ ```
237
+
238
+ Alternatively, the current index can be accessed via the `inputs` block's arguments for use anywhere:
239
+
240
+ ```erb
241
+ <%= semantic_form_for @post do |f| %>
242
+ <%= f.inputs :for => :categories do |category, i| %>
243
+ ...
244
+ <%= f.actions %>
245
+ <% end %>
246
+ ```
247
+
248
+ If you have more than one form on the same page, it may lead to HTML invalidation because of the way HTML element id attributes are assigned. You can provide a namespace for your form to ensure uniqueness of id attributes on form elements. The namespace attribute will be prefixed with underscore on the generate HTML id. For example:
249
+
250
+ ```erb
251
+ <%= semantic_form_for(@post, :namespace => 'cat_form') do |f| %>
252
+ <%= f.inputs do %>
253
+ <%= f.input :title %> # id="cat_form_post_title"
254
+ <%= f.input :body %> # id="cat_form_post_body"
255
+ <%= f.input :created_at %> # id="cat_form_post_created_at"
256
+ <% end %>
257
+ <%= f.actions %>
258
+ <% end %>
259
+ ```
260
+
261
+ Customize HTML attributes for any input using the `:input_html` option. Typically this is used to disable the input, change the size of a text field, change the rows in a textarea, or even to add a special class to an input to attach special behavior like [autogrow](http://plugins.jquery.com/project/autogrowtextarea) textareas:
262
+
263
+ ```erb
264
+ <%= semantic_form_for @post do |f| %>
265
+ <%= f.inputs do %>
266
+ <%= f.input :title, :input_html => { :size => 10 } %>
267
+ <%= f.input :body, :input_html => { :class => 'autogrow', :rows => 10, :cols => 20, :maxlength => 10 } %>
268
+ <%= f.input :created_at, :input_html => { :disabled => true } %>
269
+ <%= f.input :updated_at, :input_html => { :readonly => true } %>
270
+ <% end %>
271
+ <%= f.actions %>
272
+ <% end %>
273
+ ```
274
+
275
+ The same can be done for actions with the `:button_html` option:
276
+
277
+ ```erb
278
+ <%= semantic_form_for @post do |f| %>
279
+ ...
280
+ <%= f.actions do %>
281
+ <%= f.action :submit, :button_html => { :class => "primary", :disable_with => 'Wait...' } %>
282
+ <% end %>
283
+ <% end %>
284
+ ```
285
+
286
+ Customize the HTML attributes for the `<li>` wrapper around every input with the `:wrapper_html` option hash. There's one special key in the hash: (`:class`), which will actually _append_ your string of classes to the existing classes provided by Formtastic (like `"required string error"`).
287
+
288
+ ```erb
289
+ <%= semantic_form_for @post do |f| %>
290
+ <%= f.inputs do %>
291
+ <%= f.input :title, :wrapper_html => { :class => "important" } %>
292
+ <%= f.input :body %>
293
+ <%= f.input :description, :wrapper_html => { :style => "display:none;" } %>
294
+ <% end %>
295
+ ...
296
+ <% end %>
297
+ ```
298
+
299
+ Many inputs provide a collection of options to choose from (like `:select`, `:radio`, `:check_boxes`, `:boolean`). In many cases, Formtastic can find choices through the model associations, but if you want to use your own set of choices, the `:collection` option is what you want. You can pass in an Array of objects, an array of Strings, a Hash... Throw almost anything at it! Examples:
300
+
301
+ ```ruby
302
+ f.input :authors, :as => :check_boxes, :collection => User.order("last_name ASC").all
303
+ f.input :authors, :as => :check_boxes, :collection => current_user.company.users.active
304
+ f.input :authors, :as => :check_boxes, :collection => [@justin, @kate]
305
+ f.input :authors, :as => :check_boxes, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
306
+ f.input :author, :as => :select, :collection => Author.all
307
+ f.input :author, :as => :select, :collection => Author.pluck(:first_name, :id)
308
+ f.input :author, :as => :select, :collection => Author.pluck(Arel.sql("CONCAT(`first_name`, ' ', `last_name`)"), :id)
309
+ f.input :author, :as => :select, :collection => Author.your_custom_scope_or_class_method
310
+ f.input :author, :as => :select, :collection => { @justin.name => @justin.id, @kate.name => @kate.id }
311
+ f.input :author, :as => :select, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
312
+ f.input :author, :as => :radio, :collection => User.all
313
+ f.input :author, :as => :radio, :collection => [@justin, @kate]
314
+ f.input :author, :as => :radio, :collection => { @justin.name => @justin.id, @kate.name => @kate.id }
315
+ f.input :author, :as => :radio, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
316
+ f.input :admin, :as => :radio, :collection => ["Yes!", "No"]
317
+ f.input :book_id, :as => :select, :collection => Hash[Book.all.map{|b| [b.name,b.id]}]
318
+ f.input :fav_book,:as => :datalist , :collection => Book.pluck(:name)
319
+ ```
320
+
321
+
322
+ ## The Available Inputs
323
+
324
+ The Formtastic input types:
325
+
326
+ * `:select` - a select menu. Default for ActiveRecord associations: `belongs_to`, `has_many`, and `has_and_belongs_to_many`.
327
+ * `:check_boxes` - a set of check_box inputs. Alternative to `:select` for ActiveRecord-associations: `has_many`, and has_and_belongs_to_many`.
328
+ * `:radio` - a set of radio inputs. Alternative to `:select` for ActiveRecord-associations: `belongs_to`.
329
+ * `:time_zone` - a select input. Default for column types: `:string` with name matching `"time_zone"`.
330
+ * `:password` - a password input. Default for column types: `:string` with name matching `"password"`.
331
+ * `:text` - a textarea. Default for column types: `:text`.
332
+ * `:date_select` - a date select. Default for column types: `:date`.
333
+ * `:datetime_select` - a date and time select. Default for column types: `:datetime` and `:timestamp`.
334
+ * `:time_select` - a time select. Default for column types: `:time`.
335
+ * `:boolean` - a checkbox. Default for column types: `:boolean`.
336
+ * `:string` - a text field. Default for column types: `:string`.
337
+ * `:number` - a text field (just like string). Default for column types: `:integer`, `:float`, and `:decimal`.
338
+ * `:file` - a file field. Default for file-attachment attributes matching: [paperclip](http://github.com/thoughtbot/paperclip) or [attachment_fu](http://github.com/technoweenie/attachment_fu).
339
+ * `:country@ - a select menu of country names. Default for column types: :string with name `"country"` - requires a *country_select* plugin to be installed.
340
+ * `:email` - a text field (just like string). Default for columns with name matching `"email"`. New in HTML5. Works on some mobile browsers already.
341
+ * `:url` - a text field (just like string). Default for columns with name matching `"url"`. New in HTML5. Works on some mobile browsers already.
342
+ * `:phone` - a text field (just like string). Default for columns with name matching `"phone"` or `"fax"`. New in HTML5.
343
+ * `:search` - a text field (just like string). Default for columns with name matching `"search"`. New in HTML5. Works on Safari.
344
+ * `:hidden` - a hidden field. Creates a hidden field (added for compatibility).
345
+ * `:range` - a slider field.
346
+ * `:datalist` - a text field with a accompanying [datalist tag](https://developer.mozilla.org/en/docs/Web/HTML/Element/datalist) which provides options for autocompletion
347
+
348
+ The comments in the code are pretty good for each of these (what it does, what the output is, what the options are, etc.) so go check it out.
349
+
350
+
351
+ ## Delegation for label lookups
352
+
353
+ Formtastic decides which label to use in the following order:
354
+
355
+ ```
356
+ 1. :label # :label => "Choose Title"
357
+ 2. Formtastic i18n # if either :label => true || i18n_lookups_by_default = true (see Internationalization)
358
+ 3. Activerecord i18n # if localization file found for the given attribute
359
+ 4. label_str_method # if nothing provided this defaults to :humanize but can be set to a custom method
360
+ ```
361
+
362
+ ## Internationalization (I18n)
363
+
364
+ ### Basic Localization
365
+
366
+ Formtastic has some neat I18n-features. ActiveRecord object names and attributes are, by default, taken from calling `@object.human_name` and `@object.human_attribute_name(attr)` respectively. There are a few words specific to Formtastic that can be translated. See `lib/locale/en.yml` for more information.
367
+
368
+ Basic localization (labels only, with ActiveRecord):
369
+
370
+ ```erb
371
+ <%= semantic_form_for @post do |f| %>
372
+ <%= f.inputs do %>
373
+ <%= f.input :title %> # => :label => I18n.t('activerecord.attributes.user.title') or 'Title'
374
+ <%= f.input :body %> # => :label => I18n.t('activerecord.attributes.user.body') or 'Body'
375
+ <%= f.input :section %> # => :label => I18n.t('activerecord.attributes.user.section') or 'Section'
376
+ <% end %>
377
+ <% end %>
378
+ ```
379
+
380
+ *Note:* This is perfectly fine if you just want your labels/attributes and/or models to be translated using *ActiveRecord I18n attribute translations*, and you don't use input hints and legends. But what if you do? And what if you don't want same labels in all forms?
381
+
382
+ ### Enhanced Localization (Formtastic I18n API)
383
+
384
+ Formtastic supports localized *labels*, *hints*, *legends*, *actions* using the I18n API for more advanced usage. Your forms can now be DRYer and more flexible than ever, and still fully localized. This is how:
385
+
386
+ *1. Enable I18n lookups by default (`config/initializers/formtastic.rb`):*
387
+
388
+ ```ruby
389
+ Formtastic::FormBuilder.i18n_lookups_by_default = true
390
+ ```
391
+
392
+ *2. Add some label-translations/variants (`config/locales/en.yml`):*
393
+
394
+ ```yml
395
+ en:
396
+ formtastic:
397
+ titles:
398
+ post_details: "Post details"
399
+ labels:
400
+ post:
401
+ title: "Your Title"
402
+ body: "Write something..."
403
+ edit:
404
+ title: "Edit title"
405
+ hints:
406
+ post:
407
+ title: "Choose a good title for your post."
408
+ body: "Write something inspiring here."
409
+ placeholders:
410
+ post:
411
+ title: "Title your post"
412
+ slug: "Leave blank for an automatically generated slug"
413
+ user:
414
+ email: "you@yours.com"
415
+ actions:
416
+ create: "Create my %{model}"
417
+ update: "Save changes"
418
+ reset: "Reset form"
419
+ cancel: "Cancel and go back"
420
+ dummie: "Launch!"
421
+ ```
422
+
423
+ *3. ...and now you'll get:*
424
+
425
+ ```erb
426
+ <%= semantic_form_for Post.new do |f| %>
427
+ <%= f.inputs do %>
428
+ <%= f.input :title %> # => :label => "Choose a title...", :hint => "Choose a good title for your post."
429
+ <%= f.input :body %> # => :label => "Write something...", :hint => "Write something inspiring here."
430
+ <%= f.input :section %> # => :label => I18n.t('activerecord.attributes.user.section') or 'Section'
431
+ <% end %>
432
+ <%= f.actions do %>
433
+ <%= f.action :submit %> # => "Create my %{model}"
434
+ <% end %>
435
+ <% end %>
436
+ ```
437
+
438
+ *4. Localized titles (a.k.a. legends):*
439
+
440
+ _Note: Slightly different because Formtastic can't guess how you group fields in a form. Legend text can be set with first (as in the sample below) specified value, or :name/:title options - depending on what flavor is preferred._
441
+
442
+ ```erb
443
+ <%= semantic_form_for @post do |f| %>
444
+ <%= f.inputs :post_details do %> # => :title => "Post details"
445
+ # ...
446
+ <% end %>
447
+ # ...
448
+ <% end %>
449
+ ```
450
+
451
+ *5. Override I18n settings:*
452
+
453
+ ```erb
454
+ <%= semantic_form_for @post do |f| %>
455
+ <%= f.inputs do %>
456
+ <%= f.input :title %> # => :label => "Choose a title...", :hint => "Choose a good title for your post."
457
+ <%= f.input :body, :hint => false %> # => :label => "Write something..."
458
+ <%= f.input :section, :label => 'Some section' %> # => :label => 'Some section'
459
+ <% end %>
460
+ <%= f.actions do %>
461
+ <%= f.action :submit, :label => :dummie %> # => "Launch!"
462
+ <% end %>
463
+ <% end %>
464
+ ```
465
+
466
+ If I18n-lookups is disabled, i.e.:
467
+
468
+ ```ruby
469
+ Formtastic::FormBuilder.i18n_lookups_by_default = false
470
+ ```
471
+
472
+ ...then you can enable I18n within the forms instead:
473
+
474
+ ```erb
475
+ <%= semantic_form_for @post do |f| %>
476
+ <%= f.inputs do %>
477
+ <%= f.input :title, :label => true %> # => :label => "Choose a title..."
478
+ <%= f.input :body, :label => true %> # => :label => "Write something..."
479
+ <%= f.input :section, :label => true %> # => :label => I18n.t('activerecord.attributes.user.section') or 'Section'
480
+ <% end %>
481
+ <%= f.actions do %>
482
+ <%= f.action :submit, :label => true %> # => "Update %{model}" (if we are in edit that is...)
483
+ <% end %>
484
+ <% end %>
485
+ ```
486
+
487
+ *6. Advanced I18n lookups*
488
+
489
+ For more flexible forms; Formtastic finds translations using a bottom-up approach taking the following variables in account:
490
+
491
+ * `MODEL`, e.g. "post"
492
+ * `ACTION`, e.g. "edit"
493
+ * `KEY/ATTRIBUTE`, e.g. "title", :my_custom_key, ...
494
+
495
+ ...in the following order:
496
+
497
+ 1. `formtastic.{titles,labels,hints,actions}.MODEL.ACTION.ATTRIBUTE` - by model and action
498
+ 2. `formtastic.{titles,labels,hints,actions}.MODEL.ATTRIBUTE` - by model
499
+ 3. `formtastic.{titles,labels,hints,actions}.ATTRIBUTE` - global default
500
+
501
+ ...which means that you can define translations like this:
502
+
503
+ ```yml
504
+ en:
505
+ formtastic:
506
+ labels:
507
+ title: "Title" # Default global value
508
+ article:
509
+ body: "Article content"
510
+ post:
511
+ new:
512
+ title: "Choose a title..."
513
+ body: "Write something..."
514
+ edit:
515
+ title: "Edit title"
516
+ body: "Edit body"
517
+ ```
518
+
519
+ Values for `labels`/`hints`/`actions` are can take values: `String` (explicit value), `Symbol` (i18n-lookup-key relative to the current "type", e.g. actions:), `true` (force I18n lookup), `false` (force no I18n lookup). Titles (legends) can only take: `String` and `Symbol` - true/false have no meaning.
520
+
521
+ *7. Basic Translations*
522
+ If you want some basic translations, take a look on the [formtastic_i18n gem](https://github.com/timoschilling/formtastic_i18n).
523
+
524
+ ## Semantic errors
525
+
526
+ You can show errors on base (by default) and any other attribute just by passing its name to the semantic_errors method:
527
+
528
+ ```erb
529
+ <%= semantic_form_for @post do |f| %>
530
+ <%= f.semantic_errors :state %>
531
+ <% end %>
532
+ ```
533
+
534
+
535
+ ## Modified & Custom Inputs
536
+
537
+ You can modify existing inputs, subclass them, or create your own from scratch. Here's the basic process:
538
+
539
+ * Run the input generator and provide your custom input name. For example, `rails generate formtastic:input hat_size`. This creates the file `app/inputs/hat_size_input.rb`. You can also provide namespace to input name like `rails generate formtastic:input foo/custom` or `rails generate formtastic:input Foo::Custom`, this will create the file `app/inputs/foo/custom_input.rb` in both cases.
540
+ * To use that input, leave off the word "input" in your `as` statement. For example, `f.input(:size, :as => :hat_size)`
541
+
542
+ Specific examples follow.
543
+
544
+ ### Changing Existing Input Behavior
545
+
546
+ To modify the behavior of `StringInput`, subclass it in a new file, `app/inputs/string_input.rb`:
547
+
548
+ ```ruby
549
+ class StringInput < Formtastic::Inputs::StringInput
550
+ def to_html
551
+ puts "this is my modified version of StringInput"
552
+ super
553
+ end
554
+ end
555
+ ```
556
+
557
+ Another way to modify behavior is by using the input generator:
558
+ ```shell
559
+ $ rails generate formtastic:input string --extend
560
+ ```
561
+
562
+ This generates the file `app/inputs/string_input.rb` with its respective content class.
563
+
564
+ You can use your modified version with `:as => :string`.
565
+
566
+ ### Creating New Inputs Based on Existing Ones
567
+
568
+ To create your own new types of inputs based on existing inputs, the process is similar. For example, to create `FlexibleTextInput` based on `StringInput`, put the following in `app/inputs/flexible_text_input.rb`:
569
+
570
+ ```ruby
571
+ class FlexibleTextInput < Formtastic::Inputs::StringInput
572
+ def input_html_options
573
+ super.merge(:class => "flexible-text-area")
574
+ end
575
+ end
576
+ ```
577
+
578
+ You can also extend existing input behavior by using the input generator:
579
+
580
+ ```shell
581
+ $ rails generate formtastic:input FlexibleText --extend string
582
+ ```
583
+
584
+ This generates the file `app/inputs/flexible_text_input.rb` with its respective content class.
585
+
586
+ You can use your new input with `:as => :flexible_text`.
587
+
588
+ ### Creating New Inputs From Scratch
589
+
590
+ To create a custom `DatePickerInput` from scratch, put the following in `app/inputs/date_picker_input.rb`:
591
+
592
+ ```ruby
593
+ class DatePickerInput
594
+ include Formtastic::Inputs::Base
595
+ def to_html
596
+ # ...
597
+ end
598
+ end
599
+ ```
600
+
601
+ You can use your new input with `:as => :date_picker`.
602
+
603
+
604
+ ## Dependencies
605
+
606
+ There are none other than Rails itself, but...
607
+
608
+ * If you want to use the `:country` input, you'll need to install the [country-select plugin](https://github.com/stefanpenner/country_select) (or any other country_select plugin with the same API). Both 1.x and 2.x are supported, but they behave differently when storing data, so please see their [upgrade notes](https://github.com/stefanpenner/country_select/blob/master/UPGRADING.md) if switching from 1.x.
609
+ * There are a bunch of development dependencies if you plan to contribute to Formtastic
610
+
611
+
612
+ ## How to contribute
613
+
614
+ * Fork the project on Github
615
+ * Install development dependencies (`bundle install` and `appraisal install`)
616
+ * Create a topic branch for your changes
617
+ * Ensure that you provide *documentation* and *test coverage* for your changes (patches won't be accepted without)
618
+ * Ensure that all tests pass (`bundle exec rake`)
619
+ * Create a pull request on Github (these are also a great place to start a conversation around a patch as early as possible)
620
+
621
+
622
+ ## Project Info
623
+
624
+ Formtastic was created by [Justin French](http://www.justinfrench.com) with contributions from around 180 awesome developers. Run `git shortlog -n -s` to see the awesome.
625
+
626
+ The project is hosted on Github: [http://github.com/justinfrench/formtastic](http://github.com/justinfrench/formtastic), where your contributions, forkings, comments, issues and feedback are greatly welcomed.
627
+
628
+ Copyright (c) 2007-2016 Justin French, released under the MIT license.
629
+