SimpleForm aims to be as flexible as possible while helping you with powerful components to create your forms. The basic goal of simple form is to not touch your way of defining the layout, this way letting you find how you find the better design for your eyes.
== Instalation
sudo gem install simple_form
== Usage
SimpleForm was designed to be customized as you need to. Basically it's a stack of components that are generated to create a complete html input for you, with label + hints + errors. The best of this is that you can add any element on this stack in any place, or even remove any of them.
It also does not aim to create a lot of different logic from the default Rails form helpers, as they do a great work by themselves. Instead, SimpleForm acts as a DSL and uses some mappings to guess which input type you are trying to create. This will let you create your own mappings and helpers to use together with SimpleForm.
This will generate an entire form with labels for user name and password as well, and render errors by default when you render the form with invalid data (after submitting for example).
So instead of a checkbox for the active attribute, you'll have a set of boolean radio buttons with yes/no options. You can do the same with :as => :select option for boolean attributes.
What if you want to create a select containing the age from 18 to 60 in your form? You can do it overriding the :collection option:
Collections can be arrays or ranges, and when a :collection is given the :select input will be rendered by default, so we don't need to pass the :as => :select option.
To deal with associations, SimpleForm can generate select inputs, a serie of radios or check boxes. Lets see how it works: imagine you have the user model that belongs to a company and has_and_belongs_to_many roles. The structure should be something like:
class User < ActiveRecord::Base
belongs_to :company
has_and_belongs_to_many :roles
end
class Company < ActiveRecord::Base
has_many :users
end
class Role < ActiveRecord::Base
has_and_belongs_to_many :users
end
Now we have the user form:
<% simple_form_for @user do |f| -%>
<p><%= f.input :name %></p>
<p><%= f.association :company %></p>
<p><%= f.association :roles %></p>
<p><%= f.button :submit %></p>
<% end -%>
Simple enough right? This is going to render a :select input for choosing the :company, and another :select input with :multiple option for the :roles. You can of course change it, to use radios and check boxes as well:
f.association :company, :as => :radio
f.association :roles, :as => :check_boxes
And you will get a set of radios to select the company and another set of check boxes for the roles. Some options are available for refining the collection for associations:
* conditions => given as conditions for retrieving the collection
* order => given as order for retrieving the collection
* scopes => named scopes to be called for retrieving the collection
All web forms need buttons, right? To help you with this, SimpleForm has a default button helper that acts as a wrapper for Rails helpers, creating submit texts using I18n. It's pretty straightforward:
<% simple_form_for @user do |f| -%>
<p><%= f.input :name %></p>
<p><%= f.button :submit %></p>
<% end -%>
This will create a input submit tag for you, using the information given to create the submit text: when you are dealing with a new object, it will look for a specific :create key using I18n; when it's an existing object, it will look for :update. Otherwise, if no object is given, :submit is the default I18n key. Button will append the human name of the object whenever possible to the text, so in the context of the form above we would have 'Create User' or 'Update User', depending on the context.
You can also pass the button text directly:
f.button :submit, 'Save User'
f.button :submit, :label => 'Save User'
As button is just a wrapper, it is actually calling your :submit_tag Rails helper with the text it provides. That said, any other option you pass will be given to :submit_tag call:
SimpleForm uses all power of I18n API to lookup labels and hints for you. To customize your forms you can create a locale file like this:
en:
simple_form:
labels:
user:
username: 'User name'
password: 'Password'
hints:
user:
username: 'User name to sign in.'
password: 'No special characters, please.'
And your forms will use this information to render labels and hints for you.
SimpleForm also lets you be more specific, separating lookups through actions. Let's say you want a different label and hint for new and edit actions, the locale file would be something like:
en:
simple_form:
labels:
user:
new:
username: 'User name'
password: 'Password'
edit:
username: 'Change user name'
password: 'Change password'
hints:
user:
new:
username: 'User name to sign in.'
password: 'No special characters, please.'
edit:
username: 'Update your user name to sign in.'
password: 'Let it blank to not change your password.'
This way SimpleForm will figure out the right translation for you, based on the action being rendered. And to be a little bit DRYer with your locale file, you can skip the model information inside it:
en:
simple_form:
labels:
username: 'User name'
password: 'Password'
hints:
username: 'User name to sign in.'
password: 'No special characters, please.'
SimpleForm will always look for a default attribute translation if no specific is found inside the model key. In addition, SimpleForm will fallback to default human_attribute_name from Rails when no other translation is found.
Finally, you can also overwrite labels and hints inside your view, just by passing the label/hint manually. This way the I18n lookup will be skipped.
There are other options that can be configured through I18n API, such as required for labels and boolean texts, you just need to overwrite the following keys:
en:
simple_form:
true: 'Yes'
false: 'No'
required:
text: 'required'
mark: '*'
Instead of using the text and mark options from required, you can also overwrite the entire required html string as follows: