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, hacking together enough Ruby to prove it could be done.

It’s better than SomeOtherFormBuilder because…

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.

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.

it’s Rails 2.3-ready (including nested forms).

it has internationalization (I18n)!

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.

there’s heaps of elements, id and class attributes for you to hook in your CSS and JS.

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:

<head>
...
<%= formtastic_stylesheet_link_tag %>
...
</head>

Usage

Forms are really boring to code… you want to get onto the good stuff as fast as possible.

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:

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:

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):

Customize HTML attributes for any input using the :input_html option. Typically his 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 textareas:

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")

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:

:country – a select menu of country names. Default for column types: :string with name "country" – requires a country_select plugin to be installed.

:hidden – a hidden field. Creates a hidden field (added for compatibility).

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.

Delegation for label lookups

Formtastic decides which label to use in the following order:

1. :label # :label => "Choose Title"
2. Formtastic i18n # if either :label => true || i18n_lookups_by_default = true (see Internationalization)
3. Activerecord i18n # if localization file found for the given attribute
4. label_str_method # if nothing provided this defaults to :humanize but can be set to a custom method

Internationalization (I18n)

Basic Localization

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.

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?

Enhanced Localization (Formtastic I18n API)

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:

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.

Configuration

Form Generator

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 you stuff. This is how you use it:

Custom Inputs

If you want to add your own input types to encapsulate your own logic or interface patterns, you can do so by subclassing SemanticFormBuilder and configuring Formtastic to use your custom builder class.

Formtastic::SemanticFormHelper.builder = MyCustomBuilder

Status

Formtastic has been in active development for about a year. We’ve just recently jumped to an 0.9 version number, signaling that we consider this a 1.0 release candidate, and that the API won’t change significantly for the 1.x series.

Dependencies

There are none, but…

if you have the ValidationReflection plugin is installed, you won’t have to specify the :required option (it checks the validations on the model instead).

if you want to use the :country input, you’ll need to install the iso-3166-country-select plugin (or any other country_select plugin with the same API).

Compatibility

I’m only testing Formtastic with the latest Rails 2.4.x stable release, and it should be fine under Rails 2.3.x as well (including nested forms). Patches are welcome to allow backwards compatibility, but I don’t have the energy!

Got TextMate?

Well…there’s a TextMate-bundle in town, dedicated to make usage of Formtastic in the TextMate editor even more of a breeze:

How to contribute

Before you send a pull request, 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.

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.

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.