|
| 1 | +=Formtastic Sneaky Preview |
| 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 | +One day, I finally had enough. Hacked together forms were easy, but awesome forms with decent semantics, rich mark-up and plenty of CSS hooks were incredibly painful. I was discouraged from doing things properly because it was do much mark-up and code to write. |
| 6 | + |
| 7 | +So I opened up a text file, and wrote a DSL for how I'd like to author forms: |
| 8 | + |
| 9 | + <% semantic_form_for @article do |form| %> |
| 10 | + |
| 11 | + <%= form.error_messages %> |
| 12 | + |
| 13 | + <% form.input_field_set "Basic" do %> |
| 14 | + <%= form.input :title %> |
| 15 | + <%= form.input :body, :rows => 20 %> |
| 16 | + <%= form.input :section_id, :include_blank => true %> |
| 17 | + <%= form.input :publication_state_id, :as => :radio %> |
| 18 | + <%= form.input :author_id, :as => :select %> |
| 19 | + <%= form.input :allow_comments, :label => "Allow commenting on this article" %> |
| 20 | + <% end %> |
| 21 | + |
| 22 | + <% form.input_field_set "Extra" do %> |
| 23 | + <%= form.input :keywords, :required => false, :hint => "Example: ruby, rails, forms" %> |
| 24 | + <%= form.input :extract, :required => false, :rows => 5 %> |
| 25 | + <%= form.input :description, :required => false, :rows => 5 %> |
| 26 | + <%= form.input :url_title, :required => false %> |
| 27 | + <% end %> |
| 28 | + |
| 29 | + <% form.button_field_set do %> |
| 30 | + <%= form.cancel_button -%> |
| 31 | + <%= form.commit_button -%> |
| 32 | + <% end %> |
| 33 | + |
| 34 | + <% end %> |
| 35 | + |
| 36 | +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 <i>Learning to Love Forms</i> <http://www.slideshare.net/AaronGustafson/learning-to-love-forms-web-directions-south-07>, and then hacked together enough Ruby to prove it could be done. |
| 37 | + |
| 38 | + |
| 39 | +==Configuration |
| 40 | + |
| 41 | +If you wish, put something like this in config/initializers/formtastic_config.rb: |
| 42 | + |
| 43 | + JustinFrench::Formtastic::SemanticFormBuilder.all_fields_required_by_default = false |
| 44 | + JustinFrench::Formtastic::SemanticFormBuilder.required_string = "(required)" |
| 45 | + JustinFrench::Formtastic::SemanticFormBuilder.optional_string = "(optional)" |
| 46 | + |
| 47 | + |
| 48 | +==Guiding Principals |
| 49 | + |
| 50 | +* great forms are at the heart of most web applications |
| 51 | +* semantically rich (and far more accessible) HTML forms really are possible with the use of appropriate elements like fieldsets & lists |
| 52 | +* sometimes more mark-up is better |
| 53 | +* stylesheet authors have more options and more control if plenty of structural mark-up, class names and element ids are provided |
| 54 | +* it should be easy to write a great form rather than a crappy form |
| 55 | +* I'm sick of re-inventing the wheel every time I need a form |
| 56 | +* applications with great data modelling and a RESTful architecture are my main consideration |
| 57 | +* there's no such thing as a silver bullet and I'll still have to hard-code some things I do |
| 58 | +* best practices and common patterns have to start somewhere |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +==Status |
| 63 | + |
| 64 | +THIS IS DEFINITELY NOT PRODUCTION-READY. THINGS ARE GOING TO CHANGE A LOT. |
| 65 | + |
| 66 | +It's incredibly opinionated, incomplete, a work in progress, messy around the edges, messy in the middle too, tightly coupled to the database, tightly coupled to "my way" of doing things and also incredibly lacking in unit tests so far, but I hope you try it and offer some suggestions and improvements any way. |
| 67 | + |
| 68 | +* Justin's Weblog: http://justinfrench.com |
| 69 | +* Justin's email: justin at indent dot com dot au |
| 70 | +* Documentation: http://formtastic.justinfrench.com |
| 71 | +* SVN repository: http://indent.svnrepository.com/svn/plugins/formtastic/trunk |
| 72 | + |
| 73 | + |
| 74 | +==Roadmap |
| 75 | + |
| 76 | +* There's plenty of TODOs in the code |
| 77 | +* get the basic form_for implementation complete, proven and *well tested* (did I mention how laborious testing form builders is?) |
| 78 | +* provide generic, reusable, stylesheets that prove this stuff works |
| 79 | +* go back and ensure it works for remote_form_for, etc |
| 80 | +* probably do a shortcut like <%= form.inputs :name, :login, :email, :bio %> for those that want the form with zero configuration |
| 81 | + |
| 82 | + |
| 83 | +==Installation |
| 84 | + |
| 85 | +You know the drill: |
| 86 | + |
| 87 | + cd path/to/your/app |
| 88 | + ./script/plugin install http://indent.svnrepository.com/svn/plugins/formtastic/trunk |
| 89 | + |
| 90 | + |
| 91 | +==Usage |
| 92 | + |
| 93 | +The smallest example: |
| 94 | + |
| 95 | + <% semantic_form_for @user do |form| %> |
| 96 | + <% form.input_field_set do %> |
| 97 | + <%= form.input :name %> |
| 98 | + <%= form.input :email %> |
| 99 | + <% end %> |
| 100 | + <% end %> |
| 101 | + |
| 102 | +With an output something like: |
| 103 | + |
| 104 | + <form id="new-article-form" action="#" method="post"> |
| 105 | + <fieldset> |
| 106 | + <legend><span>Create a new User</span></legend> |
| 107 | + <fieldset class="inputs"> |
| 108 | + <ol> |
| 109 | + <li id="user_name_input" class="required string"> |
| 110 | + <label for="user_name">Name <abbr title="required">*</abbr></label> |
| 111 | + <input type="text" id="user_name" name="user[name]" size="50" /> |
| 112 | + </li> |
| 113 | + <li id="user_email_input" class="required string"> |
| 114 | + <label for="user_email">Email <abbr title="required">*</abbr></label> |
| 115 | + <input type="text" id="user_email" name="user[email]" size="50" /> |
| 116 | + </li> |
| 117 | + </ol> |
| 118 | + </fieldset> |
| 119 | + </fieldset> |
| 120 | + </form> |
| 121 | + |
| 122 | + |
| 123 | +==What about Stylesheets? |
| 124 | + |
| 125 | +It's no where near complete, but there is a sample CSS file here: |
| 126 | +http://indent.svnrepository.com/svn/plugins/formtastic/sample/public/stylesheets/formtastic.css |
| 127 | + |
| 128 | +The plan is that a CSS file similar to this will be generated or otherwise made available to your app to give you basic vertical (labels above inputs) and horizontal (labels to the left of inputs), and then if needed, you would write new styles on top to customise the presentation to suit you. |
| 129 | + |
| 130 | + |
| 131 | +==Compatibility |
| 132 | + |
| 133 | +I'm only testing Formtastic with the latest Rails stable release (2.0.2 at this time). |
| 134 | + |
| 135 | + |
| 136 | +==But it doesn't do that thing I really need! |
| 137 | + |
| 138 | +Oh noes! It might not ever do it either. We'll see. This is not a silver bullet. I want to make the usual stuff easy, and the unusual stuff possible. That might mean that some of the inputs on your form still have to be hard-coded, but some is better than all, right? |
| 139 | + |
| 140 | +I really hope the plugin will soon be clean and extensible enough to invite others to contribute more complex input types (calendars, etc) to the core. |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | +Copyright (c) 2007 Justin French, released under the MIT license. |
0 commit comments