nuatt-formtastic 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.textile +635 -0
  3. data/lib/formtastic.rb +24 -0
  4. data/lib/formtastic/form_builder.rb +75 -0
  5. data/lib/formtastic/helpers.rb +15 -0
  6. data/lib/formtastic/helpers/buttons_helper.rb +277 -0
  7. data/lib/formtastic/helpers/errors_helper.rb +124 -0
  8. data/lib/formtastic/helpers/fieldset_wrapper.rb +62 -0
  9. data/lib/formtastic/helpers/file_column_detection.rb +16 -0
  10. data/lib/formtastic/helpers/form_helper.rb +221 -0
  11. data/lib/formtastic/helpers/input_helper.rb +357 -0
  12. data/lib/formtastic/helpers/inputs_helper.rb +381 -0
  13. data/lib/formtastic/helpers/reflection.rb +12 -0
  14. data/lib/formtastic/helpers/semantic_form_helper.rb +11 -0
  15. data/lib/formtastic/html_attributes.rb +21 -0
  16. data/lib/formtastic/i18n.rb +32 -0
  17. data/lib/formtastic/inputs.rb +29 -0
  18. data/lib/formtastic/inputs/base.rb +50 -0
  19. data/lib/formtastic/inputs/base/associations.rb +33 -0
  20. data/lib/formtastic/inputs/base/choices.rb +88 -0
  21. data/lib/formtastic/inputs/base/collections.rb +94 -0
  22. data/lib/formtastic/inputs/base/database.rb +17 -0
  23. data/lib/formtastic/inputs/base/errors.rb +58 -0
  24. data/lib/formtastic/inputs/base/fileish.rb +23 -0
  25. data/lib/formtastic/inputs/base/grouped_collections.rb +77 -0
  26. data/lib/formtastic/inputs/base/hints.rb +31 -0
  27. data/lib/formtastic/inputs/base/html.rb +51 -0
  28. data/lib/formtastic/inputs/base/labelling.rb +53 -0
  29. data/lib/formtastic/inputs/base/naming.rb +54 -0
  30. data/lib/formtastic/inputs/base/options.rb +18 -0
  31. data/lib/formtastic/inputs/base/stringish.rb +30 -0
  32. data/lib/formtastic/inputs/base/timeish.rb +125 -0
  33. data/lib/formtastic/inputs/base/validations.rb +125 -0
  34. data/lib/formtastic/inputs/base/wrapping.rb +38 -0
  35. data/lib/formtastic/inputs/boolean_input.rb +87 -0
  36. data/lib/formtastic/inputs/check_boxes_input.rb +169 -0
  37. data/lib/formtastic/inputs/country_input.rb +66 -0
  38. data/lib/formtastic/inputs/date_input.rb +14 -0
  39. data/lib/formtastic/inputs/datetime_input.rb +9 -0
  40. data/lib/formtastic/inputs/email_input.rb +40 -0
  41. data/lib/formtastic/inputs/file_input.rb +42 -0
  42. data/lib/formtastic/inputs/hidden_input.rb +66 -0
  43. data/lib/formtastic/inputs/number_input.rb +72 -0
  44. data/lib/formtastic/inputs/numeric_input.rb +20 -0
  45. data/lib/formtastic/inputs/password_input.rb +40 -0
  46. data/lib/formtastic/inputs/phone_input.rb +41 -0
  47. data/lib/formtastic/inputs/radio_input.rb +146 -0
  48. data/lib/formtastic/inputs/search_input.rb +40 -0
  49. data/lib/formtastic/inputs/select_input.rb +208 -0
  50. data/lib/formtastic/inputs/string_input.rb +34 -0
  51. data/lib/formtastic/inputs/text_input.rb +47 -0
  52. data/lib/formtastic/inputs/time_input.rb +14 -0
  53. data/lib/formtastic/inputs/time_zone_input.rb +48 -0
  54. data/lib/formtastic/inputs/url_input.rb +40 -0
  55. data/lib/formtastic/localized_string.rb +96 -0
  56. data/lib/formtastic/railtie.rb +12 -0
  57. data/lib/formtastic/semantic_form_builder.rb +11 -0
  58. data/lib/formtastic/util.rb +25 -0
  59. data/lib/generators/formtastic/form/form_generator.rb +95 -0
  60. data/lib/generators/formtastic/install/install_generator.rb +23 -0
  61. data/lib/generators/templates/_form.html.erb +7 -0
  62. data/lib/generators/templates/_form.html.haml +5 -0
  63. data/lib/generators/templates/formtastic.css +145 -0
  64. data/lib/generators/templates/formtastic.rb +74 -0
  65. data/lib/generators/templates/formtastic_changes.css +14 -0
  66. data/lib/locale/en.yml +7 -0
  67. data/lib/tasks/verify_rcov.rb +44 -0
  68. metadata +206 -19
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008-2010 Justin French
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.
@@ -0,0 +1,635 @@
1
+ h1. Formtastic
2
+
3
+ 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.
4
+
5
+ <a href='http://www.pledgie.com/campaigns/2178'><img alt='Click here to lend your support to: formtastic and make a donation at www.pledgie.com !' src='http://pledgie.com/campaigns/2178.png?skin_name=chrome' border='0' /></a>
6
+
7
+
8
+ h2. Compatibility
9
+
10
+ * Formtastic 1.x is compatible with both Rails 2 and 3, and is being maintained for bug fixes in the the "1.2-stable branch":https://github.com/justinfrench/formtastic/tree/1.2-stable.
11
+ * Formtastic 2.0 will be Rails 3.x compatible only
12
+ * If you're bundling Formtastic from edge/master, get ready for a bumpy ride. Try the "1.2-stable branch":https://github.com/justinfrench/formtastic/tree/1.2-stable.
13
+ * 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!
14
+
15
+
16
+ h2. The Story
17
+
18
+ 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:
19
+
20
+ <pre>
21
+ <% semantic_form_for @article do |form| %>
22
+
23
+ <% form.inputs :name => "Basic" do %>
24
+ <%= form.input :title %>
25
+ <%= form.input :body %>
26
+ <%= form.input :section %>
27
+ <%= form.input :publication_state, :as => :radio %>
28
+ <%= form.input :category %>
29
+ <%= form.input :allow_comments, :label => "Allow commenting on this article" %>
30
+ <% end %>
31
+
32
+ <% form.inputs :name => "Advanced" do %>
33
+ <%= form.input :keywords, :required => false, :hint => "Example: ruby, rails, forms" %>
34
+ <%= form.input :extract, :required => false %>
35
+ <%= form.input :description, :required => false %>
36
+ <%= form.input :url_title, :required => false %>
37
+ <% end %>
38
+
39
+ <% form.inputs :name => "Author", :for => :author do |author_form| %>
40
+ <%= author_form.input :first_name %>
41
+ <%= author_form.input :last_name %>
42
+ <% end %>
43
+
44
+ <% form.buttons do %>
45
+ <%= form.commit_button %>
46
+ <% end %>
47
+
48
+ <% end %>
49
+ </pre>
50
+
51
+ 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.
52
+
53
+
54
+ h2. It's better than _SomeOtherFormBuilder_ because...
55
+
56
+ * 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.
57
+ * 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.
58
+ * it's Rails 3 compatible (including nested forms).
59
+ * it has internationalization (I18n)!
60
+ * 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.
61
+ * there's heaps of elements, id and class attributes for you to hook in your CSS and JS.
62
+ * it handles real world stuff like inline hints, inline error messages & help text.
63
+ * 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).
64
+ * it's got absolutely awesome spec coverage.
65
+ * there's a bunch of people using and working on it (it's not just one developer building half a solution).
66
+ * it has growing HTML5 support (new inputs like email/phone/search, new attributes like required/min/max/step/placeholder)
67
+
68
+
69
+ h2. Why?
70
+
71
+ * web apps = lots of forms.
72
+ * forms are so friggin' boring to code.
73
+ * semantically rich & accessible forms really are possible.
74
+ * the "V" is way behind the "M" and "C" in Rails' MVC – it's the ugly sibling.
75
+ * best practices and common patterns have to start somewhere.
76
+ * i need a challenge.
77
+
78
+
79
+ h2. Opinions
80
+
81
+ * it should be easier to do things the right way than the wrong way.
82
+ * sometimes _more mark-up_ is better.
83
+ * elements and attribute hooks are _gold_ for stylesheet authors.
84
+ * make the common things we do easy, yet still ensure uncommon things are still possible.
85
+
86
+
87
+ h2. Documentation
88
+
89
+ RDoc documentation _should_ be automatically generated after each commit and made available on the "rdoc.info website":http://rdoc.info/projects/justinfrench/formtastic.
90
+
91
+
92
+ h2. Installation
93
+
94
+ Formtastic edge (2.0) will be compatible with Rails 3 only, and the gem is "hosted":http://rubygems.org/gems/formtastic on RubyGems.org.
95
+
96
+ Simply add Formtastic to your Gemfile and bundle it up:
97
+
98
+ <pre>
99
+ gem 'formtastic', '~> 1.2.3'
100
+ </pre>
101
+
102
+ Optionally, run the generator to copy some stylesheets and a configuration initializer into your application:
103
+
104
+ <pre>
105
+ $ rails generate formtastic:install
106
+ </pre>
107
+
108
+
109
+ h2. Stylesheets
110
+
111
+ A proof-of-concept stylesheet is provided which you can include in your layout. Customization is best achieved by overriding these styles in an additional stylesheet so that the Formtastic styles can be updated without clobbering your changes. If you want to use these stylesheets, add both to your layout with this helper:
112
+
113
+ <pre>
114
+ <head>
115
+ ...
116
+ <%= stylesheet_link_tag 'formtastic', 'formtastic_changes' %>
117
+ ...
118
+ </head>
119
+ </pre>
120
+
121
+
122
+ h2. Usage
123
+
124
+ Forms are really boring to code... you want to get onto the good stuff as fast as possible.
125
+
126
+ This renders a set of inputs (one for _most_ columns in the database table, and one for each ActiveRecord @belongs_to@-association), followed by a submit button:
127
+
128
+ <pre>
129
+ <% semantic_form_for @user do |form| %>
130
+ <%= form.inputs %>
131
+ <%= form.buttons %>
132
+ <% end %>
133
+ </pre>
134
+
135
+ This is a great way to get something up fast, but like scaffolding, it's not recommended for production.
136
+
137
+ You probably want to specify the order of the fields, skip some of the fields or even add in fields that Formtastic couldn't detect, you can pass in a list of field names to @inputs@ and list of button names to @buttons@:
138
+
139
+ <pre>
140
+ <% semantic_form_for @user do |form| %>
141
+ <%= form.inputs :title, :body, :section, :categories, :created_at %>
142
+ <%= form.buttons :commit %>
143
+ <% end %>
144
+ </pre>
145
+
146
+ You probably want control over the input type Formtastic uses for each field, you can expand the @inputs@ and @buttons@ blocks. This specifies the @:section@ input should be a set of radio buttons (rather than the default select box), and that the @:created_at@ field should be a string (rather than the default datetime selects):
147
+
148
+ <pre>
149
+ <% semantic_form_for @post do |form| %>
150
+ <% form.inputs do %>
151
+ <%= form.input :title %>
152
+ <%= form.input :body %>
153
+ <%= form.input :section, :as => :radio %>
154
+ <%= form.input :categories %>
155
+ <%= form.input :created_at, :as => :string %>
156
+ <% end %>
157
+ <% form.buttons do %>
158
+ <%= form.commit_button %>
159
+ <% end %>
160
+ <% end %>
161
+ </pre>
162
+
163
+ 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:
164
+
165
+ <pre>
166
+ <% semantic_form_for @post do |form| %>
167
+ <% form.inputs "Basic", :id => "basic" do %>
168
+ <%= form.input :title %>
169
+ <%= form.input :body %>
170
+ <% end %>
171
+ <% form.inputs :name => "Advanced Options", :id => "advanced" do %>
172
+ <%= form.input :slug, :label => "URL Title", :hint => "Created automatically if left blank", :required => false %>
173
+ <%= form.input :section, :as => :radio %>
174
+ <%= form.input :user, :label => "Author", :label_method => :full_name %>
175
+ <%= form.input :categories, :required => false %>
176
+ <%= form.input :created_at, :as => :string, :label => "Publication Date", :required => false %>
177
+ <% end %>
178
+ <% form.buttons do %>
179
+ <%= form.commit_button %>
180
+ <% end %>
181
+ <% end %>
182
+ </pre>
183
+
184
+ You can create forms for nested resources:
185
+
186
+ <pre>
187
+ <% semantic_form_for [@author, @post] do |form| %>
188
+ </pre>
189
+
190
+ 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:
191
+
192
+ <pre>
193
+ <% semantic_form_for @post do |form| %>
194
+ <%= form.inputs :title, :body, :created_at %>
195
+ <% form.semantic_fields_for :author do |author| %>
196
+ <%= author.inputs :first_name, :last_name, :name => "Author" %>
197
+ <% end %>
198
+ <%= form.buttons %>
199
+ <% end %>
200
+ </pre>
201
+
202
+ Or the Formtastic way with the @:for@ option:
203
+
204
+ <pre>
205
+ <% semantic_form_for @post do |form| %>
206
+ <%= form.inputs :title, :body, :created_at %>
207
+ <%= form.inputs :first_name, :last_name, :for => :author, :name => "Author" %>
208
+ <%= form.buttons %>
209
+ <% end %>
210
+ </pre>
211
+
212
+ When working in has many association, you can even supply @"%i"@ in your fieldset name that it will be properly interpolated with the child index. For example:
213
+
214
+ <pre>
215
+ <% semantic_form_for @post do |form| %>
216
+ <%= form.inputs %>
217
+ <%= form.inputs :name => 'Category #%i', :for => :categories %>
218
+ <%= form.buttons %>
219
+ <% end %>
220
+ </pre>
221
+
222
+ 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
223
+ 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:
224
+
225
+ <pre>
226
+ <% semantic_form_for(@post, :namespace => 'cat_form') do |form| %>
227
+ <%= form.input :title %> # id="cat_form_post_title"
228
+ <%= form.input :body %> # id="cat_form_post_body"
229
+ <%= form.input :created_at %> # id="cat_form_post_created_at"
230
+ <%= form.buttons %>
231
+ <% end %>
232
+ </pre>
233
+
234
+ 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/autogrow textareas:
235
+
236
+ <pre>
237
+ <% semantic_form_for @post do |form| %>
238
+ <%= form.input :title, :input_html => { :cols => 10 } %>
239
+ <%= form.input :body, :input_html => { :class => 'autogrow', :rows => 10, :cols => 20, :maxlength => 10 } %>
240
+ <%= form.input :created_at, :input_html => { :disabled => true } %>
241
+ <%= form.buttons %>
242
+ <% end %>
243
+ </pre>
244
+
245
+ The same can be done for buttons with the @:button_html@ option:
246
+
247
+ <pre>
248
+ <% semantic_form_for @post do |form| %>
249
+ ...
250
+ <% form.buttons do %>
251
+ <%= form.commit_button :button_html => { :class => "primary" } %>
252
+ <% end %>
253
+ <% end %>
254
+ </pre>
255
+
256
+ 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"@).
257
+
258
+ <pre>
259
+ <% semantic_form_for @post do |form| %>
260
+ <%= form.input :title, :wrapper_html => { :class => "important" } %>
261
+ <%= form.input :body %>
262
+ <%= form.input :description, :wrapper_html => { :style => "display:none;" } %>
263
+ ...
264
+ <% end %>
265
+ </pre>
266
+
267
+ Customize the default class used for hints on each attribute or globally in the @config/formtastic.rb@ file. Similarly you can customize the error classes on an attribute level or globally.
268
+
269
+ <pre>
270
+ <% semantic_form_for @post do |form| %>
271
+ <%= form.input :title, :hint_class => 'custom-html-class', :error_class => 'custom-error-class' %>
272
+ <% end %>
273
+ </pre>
274
+
275
+ 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:
276
+
277
+ <pre>
278
+ f.input :authors, :as => :check_boxes, :collection => User.find(:all, :order => "last_name ASC")
279
+ f.input :authors, :as => :check_boxes, :collection => current_user.company.users.active
280
+ f.input :authors, :as => :check_boxes, :collection => [@justin, @kate]
281
+ f.input :authors, :as => :check_boxes, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
282
+ f.input :author, :as => :select, :collection => Author.find(:all)
283
+ f.input :author, :as => :select, :collection => { @justin.name => @justin.id, @kate.name => @kate.id }
284
+ f.input :author, :as => :select, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
285
+ f.input :author, :as => :radio, :collection => User.find(:all)
286
+ f.input :author, :as => :radio, :collection => [@justin, @kate]
287
+ f.input :author, :as => :radio, :collection => { @justin.name => @justin.id, @kate.name => @kate.id }
288
+ f.input :author, :as => :radio, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
289
+ f.input :admin, :as => :radio, :collection => ["Yes!", "No"]
290
+ </pre>
291
+
292
+
293
+ h2. The Available Inputs
294
+
295
+ The Formtastic input types:
296
+
297
+ * @:select@ - a select menu. Default for ActiveRecord associations: @belongs_to@, @has_many@, and @has_and_belongs_to_many@.
298
+ * @:check_boxes@ - a set of check_box inputs. Alternative to @:select@ for ActiveRecord-associations: @has_many@, and @has_and_belongs_to_many@.
299
+ * @:radio@ - a set of radio inputs. Alternative to @:select@ for ActiveRecord-associations: @belongs_to@.
300
+ * @:time_zone@ - a select input. Default for column types: @:string@ with name matching @"time_zone"@.
301
+ * @:password@ - a password input. Default for column types: @:string@ with name matching @"password"@.
302
+ * @:text@ - a textarea. Default for column types: @:text@.
303
+ * @:date@ - a date select. Default for column types: @:date@.
304
+ * @:datetime@ - a date and time select. Default for column types: @:datetime@ and @:timestamp@.
305
+ * @:time@ - a time select. Default for column types: @:time@.
306
+ * @:boolean@ - a checkbox. Default for column types: @:boolean@.
307
+ * @:string@ - a text field. Default for column types: @:string@.
308
+ * @:number@ - a text field (just like string). Default for column types: @:integer@, @:float@, and @:decimal@.
309
+ * @: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.
310
+ * @:country@ - a select menu of country names. Default for column types: :string with name @"country"@ - requires a *country_select* plugin to be installed.
311
+ * @:email@ - a text field (just like string). Default for columns with name matching @"email"@. New in HTML5. Works on some mobile browsers already.
312
+ * @:url@ - a text field (just like string). Default for columns with name matching @"url"@. New in HTML5. Works on some mobile browsers already.
313
+ * @:phone@ - a text field (just like string). Default for columns with name matching @"phone"@ or @"fax"@. New in HTML5.
314
+ * @:search@ - a text field (just like string). Default for columns with name matching @"search"@. New in HTML5. Works on Safari.
315
+ * @:hidden@ - a hidden field. Creates a hidden field (added for compatibility).
316
+
317
+ 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.
318
+
319
+
320
+ h2. Delegation for label lookups
321
+
322
+ Formtastic decides which label to use in the following order:
323
+
324
+ <pre>
325
+ 1. :label # :label => "Choose Title"
326
+ 2. Formtastic i18n # if either :label => true || i18n_lookups_by_default = true (see Internationalization)
327
+ 3. Activerecord i18n # if localization file found for the given attribute
328
+ 4. label_str_method # if nothing provided this defaults to :humanize but can be set to a custom method
329
+ </pre>
330
+
331
+ h2. Internationalization (I18n)
332
+
333
+ h3. Basic Localization
334
+
335
+ Formtastic got 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.
336
+
337
+ Basic localization (labels only, with ActiveRecord):
338
+
339
+ <pre>
340
+ <% semantic_form_for @post do |form| %>
341
+ <%= form.input :title %> # => :label => I18n.t('activerecord.attributes.user.title') or 'Title'
342
+ <%= form.input :body %> # => :label => I18n.t('activerecord.attributes.user.body') or 'Body'
343
+ <%= form.input :section %> # => :label => I18n.t('activerecord.attributes.user.section') or 'Section'
344
+ <% end %>
345
+ </pre>
346
+
347
+ *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?
348
+
349
+ h3. Enhanced Localization (Formtastic I18n API)
350
+
351
+ 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:
352
+
353
+ *1. Enable I18n lookups by default (@config/initializers/formtastic.rb@):*
354
+
355
+ <pre>
356
+ Formtastic::FormBuilder.i18n_lookups_by_default = true
357
+ </pre>
358
+
359
+ *2. Add some cool label-translations/variants (@config/locale/en.yml@):*
360
+
361
+ <pre>
362
+ en:
363
+ formtastic:
364
+ titles:
365
+ post_details: "Post details"
366
+ labels:
367
+ post:
368
+ title: "Your Title"
369
+ body: "Write something..."
370
+ edit:
371
+ title: "Edit title"
372
+ hints:
373
+ post:
374
+ title: "Choose a good title for you post."
375
+ body: "Write something inspiring here."
376
+ placeholders:
377
+ post:
378
+ title: "Title your post"
379
+ slug: "Leave blank for an automatically generated slug"
380
+ user:
381
+ email: "you@yours.com"
382
+ actions:
383
+ create: "Create my %{model}"
384
+ update: "Save changes"
385
+ dummie: "Launch!"
386
+ </pre>
387
+
388
+ *Note:* We are using English here still, but you get the point.
389
+
390
+ *3. ...and now you'll get:*
391
+
392
+ <pre>
393
+ <% semantic_form_for Post.new do |form| %>
394
+ <% form.inputs do %>
395
+ <%= form.input :title %> # => :label => "Choose a title...", :hint => "Choose a good title for you post."
396
+ <%= form.input :body %> # => :label => "Write something...", :hint => "Write something inspiring here."
397
+ <%= form.input :section %> # => :label => I18n.t('activerecord.attributes.user.section') or 'Section'
398
+ <% end %>
399
+ <% form.buttons do %>
400
+ <%= form.commit_button %> # => "Create my %{model}"
401
+ <% end %>
402
+ <% end %>
403
+ </pre>
404
+
405
+ *4. Localized titles (a.k.a. legends):*
406
+
407
+ _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._
408
+
409
+ <pre>
410
+ <% semantic_form_for @post do |form| %>
411
+ <% form.inputs :post_details do %> # => :title => "Post details"
412
+ # ...
413
+ <% end %>
414
+ # ...
415
+ <% end %>
416
+ </pre>
417
+
418
+ *5. Override I18n settings:*
419
+
420
+ <pre>
421
+ <% semantic_form_for @post do |form| %>
422
+ <% form.inputs do %>
423
+ <%= form.input :title %> # => :label => "Choose a title...", :hint => "Choose a good title for you post."
424
+ <%= form.input :body, :hint => false %> # => :label => "Write something..."
425
+ <%= form.input :section, :label => 'Some section' %> # => :label => 'Some section'
426
+ <% end %>
427
+ <% form.buttons do %>
428
+ <%= form.commit_button :dummie %> # => "Launch!"
429
+ <% end %>
430
+ <% end %>
431
+ </pre>
432
+
433
+ If I18n-lookups is disabled, i.e.:
434
+
435
+ <pre>
436
+ Formtastic::FormBuilder.i18n_lookups_by_default = false
437
+ </pre>
438
+
439
+ ...then you can enable I18n within the forms instead:
440
+
441
+ <pre>
442
+ <% semantic_form_for @post do |form| %>
443
+ <% form.inputs do %>
444
+ <%= form.input :title, :label => true %> # => :label => "Choose a title..."
445
+ <%= form.input :body, :label => true %> # => :label => "Write something..."
446
+ <%= form.input :section, :label => true %> # => :label => I18n.t('activerecord.attributes.user.section') or 'Section'
447
+ <% end %>
448
+ <% form.buttons do %>
449
+ <%= form.commit_button true %> # => "Update %{model}" (if we are in edit that is...)
450
+ <% end %>
451
+ <% end %>
452
+ </pre>
453
+
454
+ *6. Advanced I18n lookups*
455
+
456
+ For more flexible forms; Formtastic find translations using a bottom-up approach taking the following variables in account:
457
+
458
+ * @MODEL@, e.g. "post"
459
+ * @ACTION@, e.g. "edit"
460
+ * @KEY/ATTRIBUTE@, e.g. "title", :my_custom_key, ...
461
+
462
+ ...in the following order:
463
+
464
+ 1. @formtastic.{titles,labels,hints,actions}.MODEL.ACTION.ATTRIBUTE@ - by model and action
465
+ 2. @formtastic.{titles,labels,hints,actions}.MODEL.ATTRIBUTE@ - by model
466
+ 3. @formtastic.{titles,labels,hints,actions}.ATTRIBUTE@ - global default
467
+
468
+ ...which means that you can define translations like this:
469
+
470
+ <pre>
471
+ en:
472
+ formtastic:
473
+ labels:
474
+ title: "Title" # Default global value
475
+ article:
476
+ body: "Article content"
477
+ post:
478
+ new:
479
+ title: "Choose a title..."
480
+ body: "Write something..."
481
+ edit:
482
+ title: "Edit title"
483
+ body: "Edit body"
484
+ </pre>
485
+
486
+ 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.
487
+
488
+
489
+ h2. Semantic errors
490
+
491
+ You can show errors on base (by default) and any other attribute just passing it name to semantic_errors method:
492
+
493
+ <pre>
494
+ <% semantic_form_for @post do |form| %>
495
+ <%= form.semantic_errors :state %>
496
+ <% end %>
497
+ </pre>
498
+
499
+
500
+ h2. Configuration
501
+
502
+ Run @rails generate formtastic:install@ to copy a commented out config file into @config/initializers/formtastic.rb@. You can "view the configuration file on GitHub":http://github.com/justinfrench/formtastic/blob/master/generators/formtastic/templates/formtastic.rb
503
+
504
+
505
+ h2. Form Generator
506
+
507
+ There's a Formtastic form code generator to make your transition to Formtastic easier. All you have to do is to *specify an existing model name*, and optionally specify view template framework (ERB/HAML), and you are good to go. *Note:* This won't overwrite any of your exsting files without confrmation.
508
+
509
+ h3. Basic usage
510
+
511
+ <pre>
512
+ $ rails generate formtastic:form ModelName
513
+ exists app/views/posts
514
+ create app/views/posts/_form.html.erb
515
+ </pre>
516
+
517
+ The ERB code is saved to a partial file, but it will not overwrite existing files without a prompt.
518
+
519
+ h3. Copying the ERB code to the clipboard instead with @--copy@
520
+
521
+ <pre>
522
+ $ rails generate formtastic:form Post --copy
523
+ </pre>
524
+
525
+ h3. Specifying HAML instead of ERB with @--haml@
526
+
527
+ <pre>
528
+ $ rails generate formtastic:form Post --haml
529
+ exists app/views/admin/posts
530
+ create app/views/admin/posts/_form.html.haml
531
+ </pre>
532
+
533
+ h3. Specifying controller namespace with @--controller@
534
+
535
+ <pre>
536
+ $ rails generate formtastic:form Post --controller admin/posts
537
+ exists app/views/admin/posts
538
+ create app/views/admin/posts/_form.html.erb
539
+ </pre>
540
+
541
+ h3. Specifying which attributes need an input
542
+
543
+ <pre>
544
+ $ rails generate formtastic:form Post title:string body:text publication_state:select
545
+ exists app/views/admin/posts
546
+ create app/views/admin/posts/_form.html.erb
547
+ </pre>
548
+
549
+
550
+
551
+ h2. Modified & Custom Inputs
552
+
553
+ If you want to change the behaviour of an input, you can subclass it in your app. For example, to tweak @StringInput@, create a new file @app/inputs/string_input.rb@:
554
+
555
+ <pre>
556
+ class StringInput < Formtastic::Inputs::StringInput
557
+ def to_html
558
+ puts "this is my modified version of StringInput"
559
+ super
560
+ end
561
+ end
562
+ </pre>
563
+
564
+ To create your own new types of inputs based on existing inputs, the process is similar:
565
+
566
+ <pre>
567
+ # f.input(:body, :as => :flexible_text)
568
+ class FlexibleTextInput < Formtastic::Inputs::StringTextInput
569
+ def input_html_options
570
+ super.merge(:class => "flexible-text-area")
571
+ end
572
+ end
573
+ </pre>
574
+
575
+ To create your own new types of inputs from scratch:
576
+
577
+ <pre>
578
+ # f.input(:created_at, :as => :date_picker)
579
+ class DatePicker
580
+ def input_html_options
581
+ super.merge(:class => "date-picker")
582
+ end
583
+ end
584
+ </pre>
585
+
586
+ It was previously advised to subclass Formtastic::FormBuilder to add your own inputs. This is no longer advised, and may not work as expected.
587
+
588
+
589
+ h2. Security
590
+
591
+ By default formtastic escapes html entities in both labels and hints unless a string is marked as html_safe. If you are using an older rails version which doesn't know html_safe, or you want to globally turn this feature off, you can set the following in your initializer:
592
+
593
+ Formtastic::FormBuilder.escape_html_entities_in_hints_and_labels = false
594
+
595
+
596
+ h2. Dependencies
597
+
598
+ There are none, but...
599
+
600
+ * if you have the "ValidationReflection":http://github.com/redinger/validation_reflection plugin is installed, you won't have to specify the @:required@ option (it checks the validations on the model instead).
601
+ * if you want to use the @:country@ input, you'll need to install the "country-select plugin":https://github.com/chrislerum/country_select (or any other country_select plugin with the same API).
602
+ * "rspec":http://github.com/dchelimsky/rspec/, "rspec_hpricot_matchers":http://rubyforge.org/projects/rspec-hpricot/ and "rcov":http://github.com/relevance/rcov gems (plus any of their own dependencies) are required for the test suite.
603
+
604
+
605
+ h2. How to contribute
606
+
607
+ Please ensure that you provide appropriate spec/test coverage and ensure the documentation is up-to-date. Bonus points if you perform your changes in a clean topic branch rather than master, and if you create a pull request for your changes to be discussed and reviewed.
608
+
609
+ Please also keep your commits *atomic* so that they are more likely to apply cleanly. That means that each commit should contain the smallest possible logical change. Don't commit two features at once, don't update the gemspec at the same time you add a feature, don't fix a whole bunch of whitespace in a file at the same time you change a few lines, etc, etc.
610
+
611
+ For significant changes, you may wish to discuss your idea on the Formtastic Google group before coding to ensure that your change is likely to be accepted. Formtastic relies heavily on i18n, so if you're unsure of the impact this has on your changes, please discuss them with the group.
612
+
613
+ See below for installation of a development environment.
614
+
615
+
616
+ h2. Maintainers & Contributors
617
+
618
+ Formtastic is maintained by "Justin French":http://github.com/justinfrench, "Morton Jonuschat":http://github.com/yabawock and "Gabriel Sobrinho":http://github.com/sobrinho. "Denis Major":http://github.com/denismajor1 is doing some amazing documentation work in the wiki, and we very much appreciate the past efforts of "José Valim":http://github.com/josevalim and "Jonas Grimfelt":http://github.com/grimen and over 40 other contributors.
619
+
620
+ @git shortlog -n -s --no-merges@
621
+
622
+
623
+ h2. Google Group, Twitter, etc
624
+
625
+ Please join the "Formtastic Google Group":http://groups.google.com.au/group/formtastic, especially if you'd like to talk about a new feature, or report a bug.
626
+
627
+ You can also "follow @formtastic on Twitter":http://twitter.com/formtastic for announcements, tutorials and awesome Formtastic links.
628
+
629
+
630
+ h2. Project Info
631
+
632
+ Formtastic is hosted on Github: "http://github.com/justinfrench/formtastic":http://github.com/justinfrench/formtastic, where your contributions, forkings, comments and feedback are greatly welcomed.
633
+
634
+
635
+ Copyright (c) 2007-2010 Justin French, released under the MIT license.