README.textile 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. h1. Formtastic
  2. 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.
  3. <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>
  4. h2. Compatibility
  5. * Formtastic 2.1.x is Rails 3.x compatible
  6. * Formtastic 2.0.x is Rails 3.0.x and 3.1.x compatible only
  7. * 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. View the README in that branch for installation instructions, etc.
  8. * 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!
  9. h2. The Story
  10. 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:
  11. <pre>
  12. <%= semantic_form_for @article do |f| %>
  13. <%= f.inputs :name => "Basic" do %>
  14. <%= f.input :title %>
  15. <%= f.input :body %>
  16. <%= f.input :section %>
  17. <%= f.input :publication_state, :as => :radio %>
  18. <%= f.input :category %>
  19. <%= f.input :allow_comments, :label => "Allow commenting on this article" %>
  20. <% end %>
  21. <%= f.inputs :name => "Advanced" do %>
  22. <%= f.input :keywords, :required => false, :hint => "Example: ruby, rails, forms" %>
  23. <%= f.input :extract, :required => false %>
  24. <%= f.input :description, :required => false %>
  25. <%= f.input :url_title, :required => false %>
  26. <% end %>
  27. <%= f.inputs :name => "Author", :for => :author do |author_form| %>
  28. <%= author_form.input :first_name %>
  29. <%= author_form.input :last_name %>
  30. <% end %>
  31. <%= f.actions do %>
  32. <%= f.action :submit, :as => :button %>
  33. <%= f.action :cancel, :as => :link %>
  34. <% end %>
  35. <% end %>
  36. </pre>
  37. 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.
  38. h2. It's awesome because...
  39. * 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.
  40. * 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.
  41. * It's Rails 3 compatible (including nested forms).
  42. * It has internationalization (I18n)!
  43. * 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.
  44. * There's heaps of elements, id and class attributes for you to hook in your CSS and JS.
  45. * It handles real world stuff like inline hints, inline error messages & help text.
  46. * 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).
  47. * It's got absolutely awesome spec coverage.
  48. * There's a bunch of people using and working on it (it's not just one developer building half a solution).
  49. * It has growing HTML5 support (new inputs like email/phone/search, new attributes like required/min/max/step/placeholder)
  50. h2. Opinions
  51. * It should be easier to do things the right way than the wrong way.
  52. * Sometimes _more mark-up_ is better.
  53. * Elements and attribute hooks are _gold_ for stylesheet authors.
  54. * Make the common things we do easy, yet ensure uncommon things are still possible.
  55. h2. Documentation
  56. RDoc documentation _should_ be automatically generated after each commit and made available on the "rdoc.info website":http://rdoc.info/projects/justinfrench/formtastic.
  57. h2. Installation
  58. Simply add Formtastic to your Gemfile and bundle it up:
  59. <pre>
  60. gem 'formtastic'
  61. </pre>
  62. Run the installation generator:
  63. <pre>
  64. $ rails generate formtastic:install
  65. </pre>
  66. h2. Stylesheets
  67. 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.
  68. h3. Stylesheet usage in Rails < 3.1:
  69. <pre>
  70. $ rails generate formtastic:install
  71. </pre>
  72. <pre>
  73. # app/views/layouts/application.html.erb
  74. <%= stylesheet_link_tag 'formtastic', 'my_formtastic_changes' %>
  75. <!--[if IE 6]><%= stylesheet_link_tag 'formtastic_ie6' %><![endif]-->
  76. <!--[if IE 7]><%= stylesheet_link_tag 'formtastic_ie7' %><![endif]-->
  77. </pre>
  78. h3. Stylesheet usage in Rails >= 3.1:
  79. 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 accross to the host application. Formtastic makes three stylesheets available as an Engine, you just need to require them in your global stylesheets.
  80. <pre>
  81. # app/assets/stylesheets/application.css
  82. *= require formtastic
  83. *= require my_formtastic_changes
  84. # app/assets/stylesheets/ie6.css
  85. *= require formtastic_ie6
  86. # app/assets/stylesheets/ie7.css
  87. *= require formtastic_ie7
  88. </pre>
  89. <pre>
  90. # app/views/layouts/application.html.erb
  91. <%= stylesheet_link_tag 'application' %>
  92. <!--[if IE 6]><%= stylesheet_link_tag 'ie6' %><![endif]-->
  93. <!--[if IE 7]><%= stylesheet_link_tag 'ie7' %><![endif]-->
  94. </pre>
  95. h2. Usage
  96. Forms are really boring to code... you want to get onto the good stuff as fast as possible.
  97. 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):
  98. <pre>
  99. <%= semantic_form_for @user do |f| %>
  100. <%= f.inputs %>
  101. <%= f.actions %>
  102. <% end %>
  103. </pre>
  104. This is a great way to get something up fast, but like scaffolding, it's *not recommended for production*. Don't be so lazy!
  105. 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@:
  106. <pre>
  107. <%= semantic_form_for @user do |f| %>
  108. <%= f.inputs :title, :body, :section, :categories, :created_at %>
  109. <%= f.actions :submit, :cancel %>
  110. <% end %>
  111. </pre>
  112. 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:
  113. <pre>
  114. <%= semantic_form_for @post do |f| %>
  115. <%= f.inputs do %>
  116. <%= f.input :title %>
  117. <%= f.input :body %>
  118. <%= f.input :section, :as => :radio %>
  119. <%= f.input :categories %>
  120. <%= f.input :created_at, :as => :string %>
  121. <% end %>
  122. <%= f.actions do %>
  123. <%= f.action :submit, :as => :button %>
  124. <%= f.action :cancel, :as => :link %>
  125. <% end %>
  126. <% end %>
  127. </pre>
  128. 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:
  129. <pre>
  130. <%= semantic_form_for @post do |f| %>
  131. <%= f.inputs "Basic", :id => "basic" do %>
  132. <%= f.input :title %>
  133. <%= f.input :body %>
  134. <% end %>
  135. <%= f.inputs :name => "Advanced Options", :id => "advanced" do %>
  136. <%= f.input :slug, :label => "URL Title", :hint => "Created automatically if left blank", :required => false %>
  137. <%= f.input :section, :as => :radio %>
  138. <%= f.input :user, :label => "Author", :member_label => :full_name %>
  139. <%= f.input :categories, :required => false %>
  140. <%= f.input :created_at, :as => :string, :label => "Publication Date", :required => false %>
  141. <% end %>
  142. <%= f.actions do %>
  143. <%= f.action :submit %>
  144. <% end %>
  145. <% end %>
  146. </pre>
  147. You can create forms for nested resources:
  148. <pre>
  149. <%= semantic_form_for [@author, @post] do |f| %>
  150. </pre>
  151. 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:
  152. <pre>
  153. <%= semantic_form_for @post do |f| %>
  154. <%= f.inputs :title, :body, :created_at %>
  155. <%= f.semantic_fields_for :author do |author| %>
  156. <%= author.inputs :first_name, :last_name, :name => "Author" %>
  157. <% end %>
  158. <%= f.actions %>
  159. <% end %>
  160. </pre>
  161. Or the Formtastic way with the @:for@ option:
  162. <pre>
  163. <%= semantic_form_for @post do |f| %>
  164. <%= f.inputs :title, :body, :created_at %>
  165. <%= f.inputs :first_name, :last_name, :for => :author, :name => "Author" %>
  166. <%= f.actions %>
  167. <% end %>
  168. </pre>
  169. 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:
  170. <pre>
  171. <%= semantic_form_for @post do |f| %>
  172. <%= f.inputs %>
  173. <%= f.inputs :name => 'Category #%i', :for => :categories %>
  174. <%= f.actions %>
  175. <% end %>
  176. </pre>
  177. Alternatively, the current index can be accessed via the `inputs` block's arguments for use anywhere:
  178. <pre>
  179. <%= semantic_form_for @post do |f| %>
  180. <%= f.inputs :for => :categories do |category, i| %>
  181. <% if i <= 2 %>
  182. <%= f.inputs :name => "Category ##{i}" %>
  183. <% else %>
  184. <%= f.inputs :name => "Category ##{i} (optional)" %>
  185. <% end %>
  186. <% end %>
  187. <%= f.actions %>
  188. <% end %>
  189. </pre>
  190. 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:
  191. <pre>
  192. <%= semantic_form_for(@post, :namespace => 'cat_form') do |f| %>
  193. <%= f.inputs do %>
  194. <%= f.input :title %> # id="cat_form_post_title"
  195. <%= f.input :body %> # id="cat_form_post_body"
  196. <%= f.input :created_at %> # id="cat_form_post_created_at"
  197. <% end %>
  198. <%= f.actions %>
  199. <% end %>
  200. </pre>
  201. 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:
  202. <pre>
  203. <%= semantic_form_for @post do |f| %>
  204. <%= f.inputs do %>
  205. <%= f.input :title, :input_html => { :size => 10 } %>
  206. <%= f.input :body, :input_html => { :class => 'autogrow', :rows => 10, :cols => 20, :maxlength => 10 } %>
  207. <%= f.input :created_at, :input_html => { :disabled => true } %>
  208. <%= f.input :updated_at, :input_html => { :readonly => true } %>
  209. <% end %>
  210. <%= f.actions %>
  211. <% end %>
  212. </pre>
  213. The same can be done for actions with the @:button_html@ option:
  214. <pre>
  215. <%= semantic_form_for @post do |f| %>
  216. ...
  217. <%= f.actions do %>
  218. <%= f.action :submit, :button_html => { :class => "primary", :disable_with => 'Wait...' } %>
  219. <% end %>
  220. <% end %>
  221. </pre>
  222. 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"@).
  223. <pre>
  224. <%= semantic_form_for @post do |f| %>
  225. <%= f.inputs do %>
  226. <%= f.input :title, :wrapper_html => { :class => "important" } %>
  227. <%= f.input :body %>
  228. <%= f.input :description, :wrapper_html => { :style => "display:none;" } %>
  229. <% end %>
  230. ...
  231. <% end %>
  232. </pre>
  233. Customize the default class used for hints on each attribute or globally in the @config/initializers/formtastic.rb@ file. Similarly, you can customize the error classes on an attribute level or globally.
  234. <pre>
  235. <%= semantic_form_for @post do |f| %>
  236. <%= f.inputs do %>
  237. <%= f.input :title, :hint_class => 'custom-html-class', :error_class => 'custom-error-class' %>
  238. <% end %>
  239. <% end %>
  240. </pre>
  241. 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:
  242. <pre>
  243. f.input :authors, :as => :check_boxes, :collection => User.order("last_name ASC").all
  244. f.input :authors, :as => :check_boxes, :collection => current_user.company.users.active
  245. f.input :authors, :as => :check_boxes, :collection => [@justin, @kate]
  246. f.input :authors, :as => :check_boxes, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
  247. f.input :author, :as => :select, :collection => Author.all
  248. f.input :author, :as => :select, :collection => { @justin.name => @justin.id, @kate.name => @kate.id }
  249. f.input :author, :as => :select, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
  250. f.input :author, :as => :radio, :collection => User.all
  251. f.input :author, :as => :radio, :collection => [@justin, @kate]
  252. f.input :author, :as => :radio, :collection => { @justin.name => @justin.id, @kate.name => @kate.id }
  253. f.input :author, :as => :radio, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
  254. f.input :admin, :as => :radio, :collection => ["Yes!", "No"]
  255. </pre>
  256. h2. The Available Inputs
  257. The Formtastic input types:
  258. * @:select@ - a select menu. Default for ActiveRecord associations: @belongs_to@, @has_many@, and @has_and_belongs_to_many@.
  259. * @:check_boxes@ - a set of check_box inputs. Alternative to @:select@ for ActiveRecord-associations: @has_many@, and @has_and_belongs_to_many@.
  260. * @:radio@ - a set of radio inputs. Alternative to @:select@ for ActiveRecord-associations: @belongs_to@.
  261. * @:time_zone@ - a select input. Default for column types: @:string@ with name matching @"time_zone"@.
  262. * @:password@ - a password input. Default for column types: @:string@ with name matching @"password"@.
  263. * @:text@ - a textarea. Default for column types: @:text@.
  264. * @:date@ - a date select. Default for column types: @:date@.
  265. * @:datetime@ - a date and time select. Default for column types: @:datetime@ and @:timestamp@.
  266. * @:time@ - a time select. Default for column types: @:time@.
  267. * @:boolean@ - a checkbox. Default for column types: @:boolean@.
  268. * @:string@ - a text field. Default for column types: @:string@.
  269. * @:number@ - a text field (just like string). Default for column types: @:integer@, @:float@, and @:decimal@.
  270. * @: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.
  271. * @:country@ - a select menu of country names. Default for column types: :string with name @"country"@ - requires a *country_select* plugin to be installed.
  272. * @:email@ - a text field (just like string). Default for columns with name matching @"email"@. New in HTML5. Works on some mobile browsers already.
  273. * @:url@ - a text field (just like string). Default for columns with name matching @"url"@. New in HTML5. Works on some mobile browsers already.
  274. * @:phone@ - a text field (just like string). Default for columns with name matching @"phone"@ or @"fax"@. New in HTML5.
  275. * @:search@ - a text field (just like string). Default for columns with name matching @"search"@. New in HTML5. Works on Safari.
  276. * @:hidden@ - a hidden field. Creates a hidden field (added for compatibility).
  277. * @:range@ - a slider field.
  278. 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.
  279. h2. Delegation for label lookups
  280. Formtastic decides which label to use in the following order:
  281. <pre>
  282. 1. :label # :label => "Choose Title"
  283. 2. Formtastic i18n # if either :label => true || i18n_lookups_by_default = true (see Internationalization)
  284. 3. Activerecord i18n # if localization file found for the given attribute
  285. 4. label_str_method # if nothing provided this defaults to :humanize but can be set to a custom method
  286. </pre>
  287. h2. Internationalization (I18n)
  288. h3. Basic Localization
  289. 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.
  290. Basic localization (labels only, with ActiveRecord):
  291. <pre>
  292. <%= semantic_form_for @post do |f| %>
  293. <%= f.inputs do %>
  294. <%= f.input :title %> # => :label => I18n.t('activerecord.attributes.user.title') or 'Title'
  295. <%= f.input :body %> # => :label => I18n.t('activerecord.attributes.user.body') or 'Body'
  296. <%= f.input :section %> # => :label => I18n.t('activerecord.attributes.user.section') or 'Section'
  297. <% end %>
  298. <% end %>
  299. </pre>
  300. *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?
  301. h3. Enhanced Localization (Formtastic I18n API)
  302. 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:
  303. *1. Enable I18n lookups by default (@config/initializers/formtastic.rb@):*
  304. <pre>
  305. Formtastic::FormBuilder.i18n_lookups_by_default = true
  306. </pre>
  307. *2. Add some cool label-translations/variants (@config/locale/en.yml@):*
  308. <pre>
  309. en:
  310. formtastic:
  311. titles:
  312. post_details: "Post details"
  313. labels:
  314. post:
  315. title: "Your Title"
  316. body: "Write something..."
  317. edit:
  318. title: "Edit title"
  319. hints:
  320. post:
  321. title: "Choose a good title for your post."
  322. body: "Write something inspiring here."
  323. placeholders:
  324. post:
  325. title: "Title your post"
  326. slug: "Leave blank for an automatically generated slug"
  327. user:
  328. email: "you@yours.com"
  329. actions:
  330. create: "Create my %{model}"
  331. update: "Save changes"
  332. reset: "Reset form"
  333. cancel: "Cancel and go back"
  334. dummie: "Launch!"
  335. </pre>
  336. *3. ...and now you'll get:*
  337. <pre>
  338. <%= semantic_form_for Post.new do |f| %>
  339. <%= f.inputs do %>
  340. <%= f.input :title %> # => :label => "Choose a title...", :hint => "Choose a good title for your post."
  341. <%= f.input :body %> # => :label => "Write something...", :hint => "Write something inspiring here."
  342. <%= f.input :section %> # => :label => I18n.t('activerecord.attributes.user.section') or 'Section'
  343. <% end %>
  344. <%= f.actions do %>
  345. <%= f.action :submit %> # => "Create my %{model}"
  346. <% end %>
  347. <% end %>
  348. </pre>
  349. *4. Localized titles (a.k.a. legends):*
  350. _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._
  351. <pre>
  352. <%= semantic_form_for @post do |f| %>
  353. <%= f.inputs :post_details do %> # => :title => "Post details"
  354. # ...
  355. <% end %>
  356. # ...
  357. <% end %>
  358. </pre>
  359. *5. Override I18n settings:*
  360. <pre>
  361. <%= semantic_form_for @post do |f| %>
  362. <%= f.inputs do %>
  363. <%= f.input :title %> # => :label => "Choose a title...", :hint => "Choose a good title for your post."
  364. <%= f.input :body, :hint => false %> # => :label => "Write something..."
  365. <%= f.input :section, :label => 'Some section' %> # => :label => 'Some section'
  366. <% end %>
  367. <%= f.actions do %>
  368. <%= f.action :submit, :label => :dummie %> # => "Launch!"
  369. <% end %>
  370. <% end %>
  371. </pre>
  372. If I18n-lookups is disabled, i.e.:
  373. <pre>
  374. Formtastic::FormBuilder.i18n_lookups_by_default = false
  375. </pre>
  376. ...then you can enable I18n within the forms instead:
  377. <pre>
  378. <%= semantic_form_for @post do |f| %>
  379. <%= f.inputs do %>
  380. <%= f.input :title, :label => true %> # => :label => "Choose a title..."
  381. <%= f.input :body, :label => true %> # => :label => "Write something..."
  382. <%= f.input :section, :label => true %> # => :label => I18n.t('activerecord.attributes.user.section') or 'Section'
  383. <% end %>
  384. <%= f.actions do %>
  385. <%= f.action :submit, :label => true %> # => "Update %{model}" (if we are in edit that is...)
  386. <% end %>
  387. <% end %>
  388. </pre>
  389. *6. Advanced I18n lookups*
  390. For more flexible forms; Formtastic finds translations using a bottom-up approach taking the following variables in account:
  391. * @MODEL@, e.g. "post"
  392. * @ACTION@, e.g. "edit"
  393. * @KEY/ATTRIBUTE@, e.g. "title", :my_custom_key, ...
  394. ...in the following order:
  395. 1. @formtastic.{titles,labels,hints,actions}.MODEL.ACTION.ATTRIBUTE@ - by model and action
  396. 2. @formtastic.{titles,labels,hints,actions}.MODEL.ATTRIBUTE@ - by model
  397. 3. @formtastic.{titles,labels,hints,actions}.ATTRIBUTE@ - global default
  398. ...which means that you can define translations like this:
  399. <pre>
  400. en:
  401. formtastic:
  402. labels:
  403. title: "Title" # Default global value
  404. article:
  405. body: "Article content"
  406. post:
  407. new:
  408. title: "Choose a title..."
  409. body: "Write something..."
  410. edit:
  411. title: "Edit title"
  412. body: "Edit body"
  413. </pre>
  414. 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.
  415. h2. Semantic errors
  416. You can show errors on base (by default) and any other attribute just passing it name to semantic_errors method:
  417. <pre>
  418. <%= semantic_form_for @post do |f| %>
  419. <%= f.semantic_errors :state %>
  420. <% end %>
  421. </pre>
  422. h2. Modified & Custom Inputs
  423. You can modify existing inputs, subclass them, or create your own from scratch. Here's the basic process:
  424. * 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.
  425. * In that file, declare a classname ending in @Input@. For example, @class HatSizeInput@. It must have a @to_html@ method for rendering.
  426. * To use that input, leave off the word "input" in your @as@ statement. For example, @f.input(:size, :as => :hat_size)@
  427. Specific examples follow.
  428. h3. Changing Existing Input Behavior
  429. To modify the behavior of @StringInput@, subclass it in a new file, @app/inputs/string_input.rb@:
  430. <pre>
  431. class StringInput < Formtastic::Inputs::StringInput
  432. def to_html
  433. puts "this is my modified version of StringInput"
  434. super
  435. end
  436. end
  437. </pre>
  438. You can use your modified version with @:as => :string@.
  439. h3. Creating New Inputs Based on Existing Ones
  440. 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@:
  441. <pre>
  442. class FlexibleTextInput < Formtastic::Inputs::StringInput
  443. def input_html_options
  444. super.merge(:class => "flexible-text-area")
  445. end
  446. end
  447. </pre>
  448. You can use your new input with @:as => :flexible_text@.
  449. h3. Creating New Inputs From Scratch
  450. To create a custom @DatePickerInput@ from scratch, put the following in @app/inputs/date_picker_input.rb@:
  451. <pre>
  452. class DatePickerInput
  453. include Formtastic::Inputs::Base
  454. def to_html
  455. # ...
  456. end
  457. end
  458. </pre>
  459. You can use your new input with @:as => :date_picker@.
  460. h3. Don't subclass Formtastic::FormBuilder anymore
  461. It was previously recommended in Formtastic 1.x to subclass Formtastic::FormBuilder to add your own inputs. This is no longer recommended in Formtastic 2, and will not work as expected.
  462. h2. Security
  463. 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:
  464. Formtastic::FormBuilder.escape_html_entities_in_hints_and_labels = false
  465. h2. Dependencies
  466. There are none, but...
  467. * 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).
  468. * "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.
  469. h2. How to contribute
  470. * Fork the project on Github
  471. * Create a topic branch for your changes
  472. * Ensure that all tests pass (`bundle exec rake`)
  473. * Ensure that the changes in your branch are as atomic as possible
  474. * Create a pull request on Github
  475. 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.
  476. h2. Google Group, Twitter, etc
  477. 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.
  478. You can also follow "@justinfrench":http://twitter.com/formtastic or "@formtastic":http://twitter.com/formtastic on Twitter for announcements, tutorials and links.
  479. h2. Project Info
  480. Formtastic was created by "Justin French":http://www.justinfrench.com with contributions from around 150 awesome developers. Run @git shortlog -n -s@ to see the awesome.
  481. 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.
  482. Copyright (c) 2007-2012 Justin French, released under the MIT license.