Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 295 lines (211 sloc) 12.462 kB
09d957d textilized the README so that it looks pretty pretty on GitHub
Justin French authored
1 h1. Formtastic Sneaky Preview
dcf8beb Initial Commit
Justin French authored
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
09d957d textilized the README so that it looks pretty pretty on GitHub
Justin French authored
5 h2. The Story
dcf8beb Initial Commit
Justin French authored
6
2d4a72b @justinfrench big update to the README
authored
7 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:
dcf8beb Initial Commit
Justin French authored
8
09d957d textilized the README so that it looks pretty pretty on GitHub
Justin French authored
9 <pre>
7e3874a @justinfrench trying to fix README formatting of <'s at the start of lines
authored
10 <% semantic_form_for @article do |form| %>
353618e @markmansour Extending Formtastic can be done by overriding the semantic_form_buil…
markmansour authored
11
0b370ba @justinfrench Renamed input_field_set() to just inputs() and added a shorthand vers…
authored
12 <% form.inputs :name => "Basic" do %>
353618e @markmansour Extending Formtastic can be done by overriding the semantic_form_buil…
markmansour authored
13 <%= form.input :title %>
14 <%= form.input :body %>
1db292f @sprsquish update README to reflect association attribute changes
sprsquish authored
15 <%= form.input :section %>
16 <%= form.input :publication_state, :as => :radio %>
17 <%= form.input :author %>
353618e @markmansour Extending Formtastic can be done by overriding the semantic_form_buil…
markmansour authored
18 <%= form.input :allow_comments, :label => "Allow commenting on this article" %>
19 <% end %>
20
0b370ba @justinfrench Renamed input_field_set() to just inputs() and added a shorthand vers…
authored
21 <% form.inputs :name => "Advanced" do %>
353618e @markmansour Extending Formtastic can be done by overriding the semantic_form_buil…
markmansour authored
22 <%= form.input :keywords, :required => false, :hint => "Example: ruby, rails, forms" %>
7e3874a @justinfrench trying to fix README formatting of <'s at the start of lines
authored
23 <%= form.input :extract, :required => false %>
24 <%= form.input :description, :required => false %>
353618e @markmansour Extending Formtastic can be done by overriding the semantic_form_buil…
markmansour authored
25 <%= form.input :url_title, :required => false %>
7e3874a @justinfrench trying to fix README formatting of <'s at the start of lines
authored
26 <% end %>
27
353618e @markmansour Extending Formtastic can be done by overriding the semantic_form_buil…
markmansour authored
28 <% form.buttons do %>
29 <%= form.commit_button %>
7e3874a @justinfrench trying to fix README formatting of <'s at the start of lines
authored
30 <% end %>
09d957d textilized the README so that it looks pretty pretty on GitHub
Justin French authored
31
dcf8beb Initial Commit
Justin French authored
32 <% end %>
09d957d textilized the README so that it looks pretty pretty on GitHub
Justin French authored
33 </pre>
34
6734cfd @justinfrench sme typos and cleanups in the readme
authored
35 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.
dcf8beb Initial Commit
Justin French authored
36
37
9af5cc8 @justinfrench Big update and re-organization of the README:
authored
38 h2. It's better than _SomeOtherFormBuilder_ because...
39
f02b136 @justinfrench rearranged and beefed up the 'it's better than...' README section
authored
40 * 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
41 * 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
42 * it's Rails 2.3-ready (including nested forms)
43 * it has internationalization (I18n)!
44 * 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
9af5cc8 @justinfrench Big update and re-organization of the README:
authored
45 * there's heaps of elements, id and class attributes for you to hook in your CSS and JS
46 * it handles real world stuff line inline hints, inline error messages, help text
47 * 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)
f02b136 @justinfrench rearranged and beefed up the 'it's better than...' README section
authored
48 * it's got absolutely awesome spec coverage
49 * there's a bunch of people using and working on it (it's not just one developer building half a solution)
9af5cc8 @justinfrench Big update and re-organization of the README:
authored
50
51
15e0bd9 updated the README a little to match last night's slides
Justin French authored
52 h2. Why?
53
54 * web apps = lots of forms
55 * forms are so friggin' boring to code
56 * semantically rich & accessible forms really are possible
57 * the "V" is way behind the "M" and "C" in Rails' MVC – it's the ugly sibling
dcf8beb Initial Commit
Justin French authored
58 * best practices and common patterns have to start somewhere
15e0bd9 updated the README a little to match last night's slides
Justin French authored
59 * i need a challenge
dcf8beb Initial Commit
Justin French authored
60
61
15e0bd9 updated the README a little to match last night's slides
Justin French authored
62 h2. Opinions
63
64 * it should be easier to do things the right way than the wrong way
65 * sometimes _more mark-up_ is better
66 * elements and attribute hooks are _gold_ for stylesheet authors
67 * make the common things we do easy, yet still ensure uncommon things are still possible
68
dcf8beb Initial Commit
Justin French authored
69
9af5cc8 @justinfrench Big update and re-organization of the README:
authored
70 h2. Usage
71
72 Forms are really boring to code... you want to get onto the good stuff as fast as possible.
73
74 This renders a set of inputs (one for _most_ columns in the database table, and one for each ActiveRecord belongs_to, has_many or has_and_belongs_to_many association) and a submit button:
75
76 <pre>
77 <% semantic_form_for @user do |form| %>
78 <%= form.inputs %>
79 <%= form.buttons %>
80 <% end %>
81 </pre>
82
83 If you 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@:
84
85 <pre>
86 <% semantic_form_for @user do |form| %>
87 <%= form.inputs :title, :body, :section, :categories, :created_at %>
88 <%= form.buttons :commit %>
89 <% end %>
90 </pre>
91
92 If you 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):
93
94 <pre>
95 <% semantic_form_for @post do |form| %>
96 <% form.inputs do %>
97 <%= form.input :title %>
98 <%= form.input :body %>
99 <%= form.input :section, :as => :radio %>
100 <%= form.input :categories %>
101 <%= form.input :created_at, :as => :string %>
102 <% end %>
103 <% form.buttons do %>
104 <%= form.commit_button %>
105 <% end %>
106 <% end %>
107 </pre>
108
109 If you want to customize the label text, or render some hint text below the field, specify which fields are required/option, or break the form into two fieldsets, the DSL is pretty comprehensive:
110
111 <pre>
112 <% semantic_form_for @post do |form| %>
113 <% form.inputs :name => "Basic", :id => "basic" do %>
114 <%= form.input :title %>
115 <%= form.input :body %>
116 <% end %>
117 <% form.inputs :name => "Advanced Options", :id => "advanced" do %>
118 <%= form.input :slug, :label => "URL Title", :hint => "Created automatically if left blank", :required => false %>
119 <%= form.input :section, :as => :radio %>
120 <%= form.input :user, :label => "Author", :label_method => :full_name, %>
121 <%= form.input :categories, :required => false %>
122 <%= form.input :created_at, :as => :string, :label => "Publication Date", :required => false %>
123 <% end %>
124 <% form.buttons do %>
125 <%= form.commit_button %>
126 <% end %>
127 <% end %>
128 </pre>
129
130 Nested forms (Rails 2.3) are also supported:
131
132 <pre>
133 <% semantic_form_for @post do |post| %>
134 <%= post.semantic_fields_for :author do |author| %>
135 <%= author.inputs %>
136 <%= end %>
137 <%= post.buttons %>
138 <% end %>
139 </pre>
140
141
5a39443 @justinfrench bulking up the documentation and readme with the different inputs ava…
authored
142 h2. The Available Inputs
143
9af5cc8 @justinfrench Big update and re-organization of the README:
authored
144 * :select (a select menu) - default for ActiveRecord associations (belongs_to, has_many, has_and_belongs_to_many)
145 * :radio (a set of radio inputs) - alternative to :select for ActiveRecord belongs_to associations
5a39443 @justinfrench bulking up the documentation and readme with the different inputs ava…
authored
146 * :password (a password input) - default for :string column types with 'password' in the method name
147 * :text (a textarea) - default for :text column types
148 * :date (a date select) - default for :date column types
149 * :datetime (a date and time select) - default for :datetime and :timestamp column types
150 * :time (a time select) - default for :time column types
151 * :boolean (a checkbox) - default for :boolean column types
2007904 @justinfrench added a new boolean_select input, for boolean fields that work better…
authored
152 * :boolean_select (a yes/no select box)
5a39443 @justinfrench bulking up the documentation and readme with the different inputs ava…
authored
153 * :string (a text field) - default for :string column types
154 * :numeric (a text field, like string) - default for :integer, :float and :decimal column types
945b76c @sprsquish Add detection for Paperclip and AttachmentFu attributes.
sprsquish authored
155 * :file (a file field) - default for paperclip or attachment_fu attributes
5a39443 @justinfrench bulking up the documentation and readme with the different inputs ava…
authored
156
9af5cc8 @justinfrench Big update and re-organization of the README:
authored
157 The documentation is pretty good for each of these (what it does, what the output is, what the options are, etc) so go check it out.
5a39443 @justinfrench bulking up the documentation and readme with the different inputs ava…
authored
158
159
160 h2. Configuration
161
162 If you wish, put something like this in config/initializers/formtastic_config.rb:
163
164 <pre>
3a32b4a @josevalim Updated README with new configuration options.
josevalim authored
165 # Set the default text field size when input is a string. Default is 50
166 Formtastic::SemanticFormBuilder.default_text_field_size = 30
167
2d4a72b @justinfrench big update to the README
authored
168 # Should all fields be considered "required" by default
169 # Defaults to true, see ValidationReflection notes below
ee26f18 @sprsquish Remove JustinFrench namespace and add deprecation message for anyone …
sprsquish authored
170 Formtastic::SemanticFormBuilder.all_fields_required_by_default = false
2d4a72b @justinfrench big update to the README
authored
171
172 # Set the string that will be appended to the labels/fieldsets which are required
3a32b4a @josevalim Updated README with new configuration options.
josevalim authored
173 # It accepts string or procs and the default is a localized version of
174 # '<abbr title="required">*</abbr>'. In other words, if you configure formtastic.required
175 # in your locale, it will replace the abbr title properly. But if you don't want to use
176 # abbr tag, you can simply give a string as below
ee26f18 @sprsquish Remove JustinFrench namespace and add deprecation message for anyone …
sprsquish authored
177 Formtastic::SemanticFormBuilder.required_string = "(required)"
2d4a72b @justinfrench big update to the README
authored
178
179 # Set the string that will be appended to the labels/fieldsets which are optional
180 # Defaults to an empty string ("")
ee26f18 @sprsquish Remove JustinFrench namespace and add deprecation message for anyone …
sprsquish authored
181 Formtastic::SemanticFormBuilder.optional_string = "(optional)"
5d072fa @andypearson Updating README to include a note about configuring inline errors
andypearson authored
182
2d4a72b @justinfrench big update to the README
authored
183 # Set the way inline errors will be displayed.
d3d53d0 @justinfrench typo fix in README
authored
184 # Defaults to :sentence, valid options are :sentence, :list and :none
2d4a72b @justinfrench big update to the README
authored
185 Formtastic::SemanticFormBuilder.inline_errors = :list
282b28c @sprsquish Add I18n support
sprsquish authored
186
2d4a72b @justinfrench big update to the README
authored
187 # Set the method to call on label text to transform or format it for human-friendly reading
3a32b4a @josevalim Updated README with new configuration options.
josevalim authored
188 # Defaults to :to_s, because we are already using human_attribute_name when creating labels,
189 # but you might want to change for :titleize or another string method
282b28c @sprsquish Add I18n support
sprsquish authored
190 Formtastic::SemanticFormBuilder.label_str_method = :titleize
30cfc86 @sprsquish Add Formtastic::SemanticFormBuilder.collection_label_methods to allow…
sprsquish authored
191
2d4a72b @justinfrench big update to the README
authored
192 # Set the array of methods to try calling on parent objects in :select and :radio inputs
193 # for the text inside each @<option>@ tag or alongside each radio @<input>@. The first method
194 # that is found on the object will be used.
195 # Defaults to ["to_label", "display_name", "full_name", "name", "title", "username", "login", "value", "to_s"]
196 Formtastic::SemanticFormBuilder.collection_label_methods = ["title_and_author", "display_name", "login", "to_s"]
5a39443 @justinfrench bulking up the documentation and readme with the different inputs ava…
authored
197 </pre>
198
199
9af5cc8 @justinfrench Big update and re-organization of the README:
authored
200 h2. Internationalization (I18n)
201
202 Supports I18n! 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.
203
204 Here is an example locale file:
205
206 <pre>
207 en:
208 formtastic:
209 yes: 'Yes'
210 no: 'No'
211 create: 'Create'
212 save: 'Save'
213 year: 'Year'
214 month: 'Month'
215 day: 'Day'
216 hour: 'Hour'
217 minute: 'Minute'
218 second: 'Second'
3a32b4a @josevalim Updated README with new configuration options.
josevalim authored
219 required: 'required'
9af5cc8 @justinfrench Big update and re-organization of the README:
authored
220 </pre>
221
222
2d4a72b @justinfrench big update to the README
authored
223 h2. ValidationReflection plugin
224
225 If you have the "ValidationReflection":http://github.com/redinger/validation_reflection plugin installed, you won't have to specify the :required option (it checks the validations on the model instead).
226
5a39443 @justinfrench bulking up the documentation and readme with the different inputs ava…
authored
227
09d957d textilized the README so that it looks pretty pretty on GitHub
Justin French authored
228 h2. Status
dcf8beb Initial Commit
Justin French authored
229
9af5cc8 @justinfrench Big update and re-organization of the README:
authored
230 *THINGS ARE GOING TO CHANGE A BIT BEFORE WE HIT 1.0.*
dcf8beb Initial Commit
Justin French authored
231
9af5cc8 @justinfrench Big update and re-organization of the README:
authored
232 It's a work in progress and a bit rough around the edges still, but I hope you try it and offer some suggestions and improvements anyway.
2d4a72b @justinfrench big update to the README
authored
233
234 On the plus side, it has a comprehensive spec suite and contributions from at least ten independent developers.
dcf8beb Initial Commit
Justin French authored
235
2d4a72b @justinfrench big update to the README
authored
236 "Wishlist":http://wiki.github.com/justinfrench/formtastic/wishlist on the wiki is serving as pretty good documentation for the roadmap to 1.0 and beyond right now, but I'll work on getting a real tracking system or something happening soon.
c5d02e6 @justinfrench link to the wiki for the roadmap to 1.0 in README
authored
237
dcf8beb Initial Commit
Justin French authored
238
9af5cc8 @justinfrench Big update and re-organization of the README:
authored
239 h2. Dependencies
dcf8beb Initial Commit
Justin French authored
240
9af5cc8 @justinfrench Big update and re-organization of the README:
authored
241 There are none, but...
dcf8beb Initial Commit
Justin French authored
242
9af5cc8 @justinfrench Big update and re-organization of the README:
authored
243 * 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)
244 * rspec, rspec_hpricot_matchers and rcov gems (plus any of their own dependencies) are required for the test suite
dcf8beb Initial Commit
Justin French authored
245
0b370ba @justinfrench Renamed input_field_set() to just inputs() and added a shorthand vers…
authored
246
9af5cc8 @justinfrench Big update and re-organization of the README:
authored
247 h2. Compatibility
0b370ba @justinfrench Renamed input_field_set() to just inputs() and added a shorthand vers…
authored
248
9af5cc8 @justinfrench Big update and re-organization of the README:
authored
249 I'm only testing Formtastic with the latest Rails 2.2.x stable release, and it should be fine under Rails 2.3 as well (including nested forms). Patches are welcome to allow backwards compatibility, but I don't have the energy!
0b370ba @justinfrench Renamed input_field_set() to just inputs() and added a shorthand vers…
authored
250
251
92e5fc3 @justinfrench Updates to the README with better examples and output samples.
authored
252
9af5cc8 @justinfrench Big update and re-organization of the README:
authored
253 h2. What about Stylesheets?
2d4a72b @justinfrench big update to the README
authored
254
fcf66e4 @justinfrench burned the 'extending Formtastic' from README... will write a simpler…
authored
255 A proof-of-concept (very much a work-in-progress) 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.
085e22f @sprsquish Add support for nested forms (http://ryandaigle.com/articles/2009/2/1…
sprsquish authored
256
9af5cc8 @justinfrench Big update and re-organization of the README:
authored
257 1. Use the generator to copy the formtastic.css and formtastic_changes.css into your public directory
085e22f @sprsquish Add support for nested forms (http://ryandaigle.com/articles/2009/2/1…
sprsquish authored
258
259 <pre>
9af5cc8 @justinfrench Big update and re-organization of the README:
authored
260 ./script/generate formtastic_stylesheets
085e22f @sprsquish Add support for nested forms (http://ryandaigle.com/articles/2009/2/1…
sprsquish authored
261 </pre>
282b28c @sprsquish Add I18n support
sprsquish authored
262
9af5cc8 @justinfrench Big update and re-organization of the README:
authored
263 2. Add both formtastic.css and formtastic_changes.css to your layout:
282b28c @sprsquish Add I18n support
sprsquish authored
264
265 <pre>
9af5cc8 @justinfrench Big update and re-organization of the README:
authored
266 <%= stylesheet_link_tag "formtastic" %>
267 <%= stylesheet_link_tag "formtastic_changes" %>
282b28c @sprsquish Add I18n support
sprsquish authored
268 </pre>
085e22f @sprsquish Add support for nested forms (http://ryandaigle.com/articles/2009/2/1…
sprsquish authored
269
2d4a72b @justinfrench big update to the README
authored
270
271 h2. Many thanks to Formtastic's contributors
ba3ce05 @justinfrench updated the README a little with contributors and some other busy work
authored
272
5a39443 @justinfrench bulking up the documentation and readme with the different inputs ava…
authored
273 * "Justin French":http://justinfrench.com
274 * "Xavier Shay":http://rhnh.net
0c50ce3 @justinfrench Updated contributors list in the README. Thanks Everyone\!
authored
275 * "Bin Dong":http://github.com/dongbin
dc3ace9 @justinfrench more on the README
authored
276 * "Ben Hamill":http://blog.benhamill.com/
15ff964 @justinfrench Added Pat and negonicrac to contributors.
authored
277 * "Pat Allan":http://github.com/freelancing-god
278 * "negonicrac":http://github.com/negonicrac
0c50ce3 @justinfrench Updated contributors list in the README. Thanks Everyone\!
authored
279 * "Andy Pearson":http://github.com/andypearson
1587c56 @justinfrench Vanity commit for Mark ;)
authored
280 * "Mark Mansour":http://stateofflux.com
0c50ce3 @justinfrench Updated contributors list in the README. Thanks Everyone\!
authored
281 * "Tien Dung":http://github.com/tiendung
c8d721e @justinfrench Updated the contributors list
authored
282 * "Sascha Hoellger":http://github.com/mitnal
283 * "Jeff Smick":http://github.com/sprsquish
3a32b4a @josevalim Updated README with new configuration options.
josevalim authored
284 * "José Valim":http://github.com/josevalim
ba3ce05 @justinfrench updated the README a little with contributors and some other busy work
authored
285
2d4a72b @justinfrench big update to the README
authored
286
ba3ce05 @justinfrench updated the README a little with contributors and some other busy work
authored
287 h2. Project Info
288
dc3ace9 @justinfrench more on the README
authored
289 Formtastic is hosted on Github: http://github.com/justinfrench/formtastic/, where your contributions, forkings, comments and feedback are greatly welcomed.
ba3ce05 @justinfrench updated the README a little with contributors and some other busy work
authored
290
324a875 @justinfrench link to the google group from the README
authored
291 There's also a newly created "Formtastic Google Group":http://groups.google.com.au/group/formtastic.
292
ba3ce05 @justinfrench updated the README a little with contributors and some other busy work
authored
293
25d0c7a @justinfrench fleshed out a 'roadmap to 1.0' in the README
authored
294 Copyright (c) 2007-2008 Justin French, released under the MIT license.
Something went wrong with that request. Please try again.