2013-02-26 13:34:19 -05:00
![Simple Form Logo ](https://raw.github.com/plataformatec/simple_form/master/simple_form.png )
2013-04-14 02:37:25 -04:00
By [Plataformatec ](http://plataformatec.com.br/ ).
2013-02-26 13:34:19 -05:00
[![Gem Version ](https://fury-badge.herokuapp.com/rb/simple_form.png )](http://badge.fury.io/rb/simple_form)
2013-04-28 12:05:17 -04:00
[![Build Status ](https://api.travis-ci.org/plataformatec/simple_form.png?branch=master )](http://travis-ci.org/plataformatec/simple_form)
2013-02-10 17:38:18 -05:00
[![Code Climate ](https://codeclimate.com/github/plataformatec/simple_form.png )](https://codeclimate.com/github/plataformatec/simple_form)
2014-02-25 12:14:17 -05:00
[![Inline docs ](http://inch-pages.github.io/github/plataformatec/simple_form.png )](http://inch-pages.github.io/github/plataformatec/simple_form)
2011-09-23 16:00:47 -04:00
2013-02-26 13:34:19 -05:00
Rails forms made easy.
2013-09-21 11:02:47 -04:00
**Simple Form** 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, letting
2012-01-31 09:31:48 -05:00
you find the better design for your eyes. Most of the DSL was inherited from Formtastic,
2012-01-27 14:13:35 -05:00
which we are thankful for and should make you feel right at home.
2011-09-23 16:00:47 -04:00
2012-02-10 08:22:18 -05:00
INFO: This README is [also available in a friendly navigable format ](http://simple-form.plataformatec.com.br/ )
2013-09-21 11:02:47 -04:00
and refers to **Simple Form** 3.0. For older releases, check the related branch for your version.
2012-01-31 09:31:48 -05:00
2011-09-23 16:00:47 -04:00
## Installation
2012-01-30 13:14:58 -05:00
Add it to your Gemfile:
2011-09-23 16:00:47 -04:00
2013-01-27 21:53:00 -05:00
```ruby
gem 'simple_form'
```
2011-09-23 16:00:47 -04:00
2012-01-30 13:14:58 -05:00
Run the following command to install it:
2011-09-23 16:00:47 -04:00
2013-01-27 21:53:00 -05:00
```console
bundle install
```
2011-09-23 16:00:47 -04:00
Run the generator:
2013-01-27 21:53:00 -05:00
```console
rails generate simple_form:install
```
2011-09-23 16:00:47 -04:00
2012-01-30 13:14:58 -05:00
Also, if you want to use the country select, you will need the
[country_select gem ](https://rubygems.org/gems/country_select ), add it to your Gemfile:
2011-09-23 16:00:47 -04:00
2013-01-27 21:53:00 -05:00
```ruby
gem 'country_select'
```
2011-09-23 16:00:47 -04:00
2013-11-08 07:38:06 -05:00
### Bootstrap
2012-01-30 11:18:22 -05:00
2014-03-21 14:39:40 -04:00
**Simple Form** can be easily integrated to the [Bootstrap ](http://getbootstrap.com/ ).
2012-01-31 07:59:32 -05:00
To do that you have to use the `bootstrap` option in the install generator, like this:
2012-01-30 11:18:22 -05:00
2013-01-27 21:53:00 -05:00
```console
rails generate simple_form:install --bootstrap
```
2012-01-30 11:18:22 -05:00
2014-03-21 14:39:40 -04:00
You have to be sure that you added a copy of the [Bootstrap ](http://getbootstrap.com/ )
2012-01-30 11:18:22 -05:00
assets on your application.
2012-01-31 07:59:32 -05:00
For more information see the generator output, our
2012-01-30 12:46:27 -05:00
[example application code ](https://github.com/rafaelfranca/simple_form-bootstrap ) and
2012-01-31 07:59:32 -05:00
[the live example app ](http://simple-form-bootstrap.plataformatec.com.br/ ).
2012-01-30 11:18:22 -05:00
2012-11-03 22:24:30 -04:00
### Zurb Foundation 3
2013-09-21 10:58:50 -04:00
To generate wrappers that are compatible with [Zurb Foundation 3 ](http://foundation.zurb.com/ ), pass
the `foundation` option to the generator, like this:
2012-11-03 22:24:30 -04:00
2013-01-27 21:53:00 -05:00
```console
rails generate simple_form:install --foundation
```
2012-11-03 22:24:30 -04:00
2013-09-21 10:58:50 -04:00
Please note that the Foundation wrapper does not support the `:hint` option by default. In order to
enable hints, please uncomment the appropriate line in `config/initializers/simple_form_foundation.rb` .
You will need to provide your own CSS styles for hints.
2012-11-04 16:16:49 -05:00
2013-03-20 05:52:42 -04:00
Please see the [instructions on how to install Foundation in a Rails app ](http://foundation.zurb.com/old-docs/f3/rails.php ).
2012-11-03 22:24:30 -04:00
2011-09-23 16:00:47 -04:00
## Usage
2013-09-21 11:02:47 -04:00
**Simple Form** was designed to be customized as you need to. Basically it's a stack of components that
2012-01-27 14:13:35 -05:00
are invoked to create a complete html input for you, which by default contains label, hints, errors
and the input itself. It does not aim to create a lot of different logic from the default Rails
2013-12-28 22:23:13 -05:00
form helpers, as they do a great job by themselves. Instead, **Simple Form** acts as a DSL and just
2012-08-28 15:14:00 -04:00
maps your input type (retrieved from the column definition in the database) to a specific helper method.
2011-09-23 16:00:47 -04:00
2013-09-21 11:02:47 -04:00
To start using **Simple Form** you just have to use the helper it provides:
2011-09-23 16:00:47 -04:00
```erb
2012-01-24 17:10:14 -05:00
< %= simple_form_for @user do |f| %>
< %= f.input :username %>
< %= f.input :password %>
< %= f.button :submit %>
< % end %>
2011-09-23 16:00:47 -04:00
```
2012-01-27 14:13:35 -05:00
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).
2011-09-23 16:00:47 -04:00
2012-01-27 15:35:43 -05:00
You can overwrite the default label by passing it to the input method. You can also add a hint or
2012-05-10 07:37:31 -04:00
even a placeholder. For boolean inputs, you can add an inline label as well:
2011-09-23 16:00:47 -04:00
```erb
2012-01-24 17:10:14 -05:00
< %= simple_form_for @user do |f| %>
2013-01-28 16:02:59 -05:00
< %= f.input :username, label: 'Your username please' %>
< %= f.input :password, hint: 'No special characters.' %>
< %= f.input :email, placeholder: 'user@domain.com' %>
< %= f.input :remember_me, inline_label: 'Yes, remember me' %>
2012-01-24 17:10:14 -05:00
< %= f.button :submit %>
< % end %>
2011-09-23 16:00:47 -04:00
```
2012-01-30 12:46:27 -05:00
In some cases you may want to disable labels, hints or error. Or you may want to configure the html
2012-01-27 15:35:43 -05:00
of any of them:
2011-09-23 16:00:47 -04:00
```erb
2012-01-24 17:10:14 -05:00
< %= simple_form_for @user do |f| %>
2013-01-28 16:02:59 -05:00
< %= f.input :username, label_html: { class: 'my_class' } %>
< %= f.input :password, hint: false, error_html: { id: 'password_error'} %>
< %= f.input :password_confirmation, label: false %>
2012-01-24 17:10:14 -05:00
< %= f.button :submit %>
< % end %>
2011-09-23 16:00:47 -04:00
```
2012-01-27 15:35:43 -05:00
It is also possible to pass any html attribute straight to the input, by using the `:input_html`
option, for instance:
2011-09-23 16:00:47 -04:00
```erb
2012-01-24 17:10:14 -05:00
< %= simple_form_for @user do |f| %>
2013-01-28 16:02:59 -05:00
< %= f.input :username, input_html: { class: 'special' } %>
< %= f.input :password, input_html: { maxlength: 20 } %>
< %= f.input :remember_me, input_html: { value: '1' } %>
2012-01-24 17:10:14 -05:00
< %= f.button :submit %>
< % end %>
2011-09-23 16:00:47 -04:00
```
2012-01-27 14:51:16 -05:00
If you want to pass the same options to all inputs in the form (for example, a default class),
you can use the `:defaults` option in `simple_form_for` . Specific options in `input` call will
2012-01-27 14:13:35 -05:00
overwrite the defaults:
2011-11-09 17:43:49 -05:00
```erb
2013-01-28 16:02:59 -05:00
< %= simple_form_for @user , defaults: { input_html: { class: 'default_class' } } do |f| %>
< %= f.input :username, input_html: { class: 'special' } %>
< %= f.input :password, input_html: { maxlength: 20 } %>
< %= f.input :remember_me, input_html: { value: '1' } %>
2012-01-24 17:10:14 -05:00
< %= f.button :submit %>
< % end %>
2011-11-09 17:43:49 -05:00
```
2013-09-21 11:02:47 -04:00
Since **Simple Form** generates a wrapper div around your label and input by default, you can pass
2012-01-27 15:35:43 -05:00
any html attribute to that wrapper as well using the `:wrapper_html` option, like so:
2011-09-28 12:51:23 -04:00
```erb
2012-01-24 17:10:14 -05:00
< %= simple_form_for @user do |f| %>
2013-01-28 16:02:59 -05:00
< %= f.input :username, wrapper_html: { class: 'username' } %>
< %= f.input :password, wrapper_html: { id: 'password' } %>
< %= f.input :remember_me, wrapper_html: { class: 'options' } %>
2012-01-24 17:10:14 -05:00
< %= f.button :submit %>
< % end %>
2011-09-28 12:51:23 -04:00
```
2012-06-26 21:51:37 -04:00
Required fields are marked with an * prepended to their labels.
2013-09-21 10:58:50 -04:00
By default all inputs are required. When the form object has `presence` validations attached to its
2013-09-21 11:02:47 -04:00
fields, **Simple Form** tells required and optional fields apart. For performance reasons, this
2013-09-21 10:58:50 -04:00
detection is skipped on validations that make use of conditional options, such as `:if` and `:unless` .
2012-06-26 21:51:37 -04:00
And of course, the `required` property of any input can be overwritten as needed:
2011-09-23 16:00:47 -04:00
```erb
2012-01-24 17:10:14 -05:00
< %= simple_form_for @user do |f| %>
2013-01-28 16:02:59 -05:00
< %= f.input :name, required: false %>
2012-01-24 17:10:14 -05:00
< %= f.input :username %>
< %= f.input :password %>
< %= f.button :submit %>
< % end %>
2011-09-23 16:00:47 -04:00
```
2014-03-14 17:24:35 -04:00
By default, **Simple Form** will look at the column type in the database and use an
2013-09-16 08:55:26 -04:00
appropriate input for the column. For example, a column created with type
`:text` in the database will use a `textarea` input by default. See the section
[Available input types and defaults for each column
type](https://github.com/plataformatec/simple_form#available-input-types-and-defaults-for-each-column-type)
for a complete list of defaults.
2014-03-14 17:24:35 -04:00
**Simple Form** also lets you overwrite the default input type it creates:
2011-09-23 16:00:47 -04:00
```erb
2012-01-24 17:10:14 -05:00
< %= simple_form_for @user do |f| %>
< %= f.input :username %>
< %= f.input :password %>
2013-01-28 16:02:59 -05:00
< %= f.input :description, as: :text %>
< %= f.input :accepts, as: :radio_buttons %>
2012-01-24 17:10:14 -05:00
< %= f.button :submit %>
< % end %>
2011-09-23 16:00:47 -04:00
```
2012-01-27 15:35:43 -05:00
So instead of a checkbox for the *accepts* attribute, you'll have a pair of radio buttons with yes/no
2013-09-16 08:54:40 -04:00
labels and a textarea instead of a text field for the description. You can also render boolean
2013-01-28 16:02:59 -05:00
attributes using `as: :select` to show a dropdown.
2011-09-23 16:00:47 -04:00
2013-09-21 11:02:47 -04:00
It is also possible to give the `:disabled` option to **Simple Form** , and it'll automatically mark
2012-01-27 15:35:43 -05:00
the wrapper as disabled with a css class, so you can style labels, hints and other components inside
2012-01-27 14:13:35 -05:00
the wrapper as well:
2011-09-23 16:00:47 -04:00
```erb
2012-01-24 17:10:14 -05:00
< %= simple_form_for @user do |f| %>
2013-01-28 16:02:59 -05:00
< %= f.input :username, disabled: true, hint: 'You cannot change your username.' %>
2012-01-24 17:10:14 -05:00
< %= f.button :submit %>
< % end %>
2011-09-23 16:00:47 -04:00
```
2013-09-21 11:02:47 -04:00
**Simple Form** accepts same options as their corresponding input type helper in Rails:
2011-09-23 16:00:47 -04:00
```erb
2012-01-24 17:10:14 -05:00
< %= simple_form_for @user do |f| %>
2013-01-28 16:02:59 -05:00
< %= f.input :date_of_birth, as: :date, start_year: Date.today.year - 90,
end_year: Date.today.year - 12, discard_day: true,
order: [:month, :year] %>
2013-12-16 14:41:37 -05:00
< %= f.input :accepts, as: :boolean, checked_value: true, unchecked_value: false %>
2012-01-24 17:10:14 -05:00
< %= f.button :submit %>
< % end %>
2011-09-23 16:00:47 -04:00
```
2013-09-21 11:02:47 -04:00
**Simple Form** also allows you to use label, hint, input_field, error and full_error helpers
2012-01-27 14:13:35 -05:00
(please take a look at the rdocs for each method for more info):
2011-09-23 16:00:47 -04:00
```erb
2012-01-24 17:10:14 -05:00
< %= simple_form_for @user do |f| %>
< %= f.label :username %>
< %= f.input_field :username %>
< %= f.hint 'No special characters, please!' %>
2013-01-28 16:02:59 -05:00
< %= f.error :username, id: 'user_name_error' %>
2012-01-24 17:10:14 -05:00
< %= f.full_error :token %>
< %= f.submit 'Save' %>
< % end %>
2011-09-23 16:00:47 -04:00
```
Any extra option passed to these methods will be rendered as html option.
2013-02-12 17:39:22 -05:00
### Stripping away all wrapper divs
2013-09-21 11:02:47 -04:00
**Simple Form** also allows you to strip away all the div wrappers around the `<input>` field that is
2013-09-21 10:58:50 -04:00
generated with the usual `f.input` .
2013-02-12 17:39:22 -05:00
The easiest way to achieve this is to use `f.input_field` .
Example:
2013-06-11 13:13:19 -04:00
```ruby
2013-02-12 17:39:22 -05:00
simple_form_for @user do |f|
f.input_field :name
2013-12-10 15:12:39 -05:00
f.input_field :remember_me, as: :boolean
2013-02-12 17:39:22 -05:00
end
```
2013-11-23 08:30:28 -05:00
```html
< form >
...
< input class = "string required" id = "user_name" maxlength = "255" name = "user[name]" size = "255" type = "text" >
< input name = "user[remember_me]" type = "hidden" value = "0" >
< label class = "checkbox" >
< input class = "boolean optional" id = "user_published" name = "user[remember_me]" type = "checkbox" value = "1" >
< / label >
< / form >
```
2013-12-15 13:58:14 -05:00
For check boxes and radio buttons you can remove the label changing `boolean_style` from default value `:nested` to `:inline` .
2013-11-23 08:30:28 -05:00
Example:
```ruby
simple_form_for @user do |f|
f.input_field :name
2013-12-10 15:12:39 -05:00
f.input_field :remember_me, as: :boolean, boolean_style: :inline
2013-11-23 08:30:28 -05:00
end
```
```html
< form >
...
< input class = "string required" id = "user_name" maxlength = "255" name = "user[name]" size = "255" type = "text" >
< input name = "user[remember_me]" type = "hidden" value = "0" >
2013-12-10 11:29:37 -05:00
< input class = "boolean optional" id = "user_remember_me" name = "user[remember_me]" type = "checkbox" value = "1" >
2013-11-23 08:30:28 -05:00
< / form >
```
2013-02-12 17:39:22 -05:00
Produces:
2013-06-11 13:13:19 -04:00
```html
2013-02-12 17:39:22 -05:00
< input class = "string required" id = "user_name" maxlength = "100"
name="user[name]" size="100" type="text" value="Carlos" />
```
To view the actual RDocs for this, check them out here - http://rubydoc.info/github/plataformatec/simple_form/master/SimpleForm/FormBuilder:input_field
2011-09-23 16:00:47 -04:00
### Collections
2012-01-27 14:13:35 -05:00
And what if you want to create a select containing the age from 18 to 60 in your form? You can do it
2012-01-27 15:35:43 -05:00
overriding the `:collection` option:
2011-09-23 16:00:47 -04:00
```erb
2012-01-24 17:10:14 -05:00
< %= simple_form_for @user do |f| %>
< %= f.input :user %>
2013-01-28 16:02:59 -05:00
< %= f.input :age, collection: 18..60 %>
2012-01-24 17:10:14 -05:00
< %= f.button :submit %>
< % end %>
2011-09-23 16:00:47 -04:00
```
2012-01-27 15:35:43 -05:00
Collections can be arrays or ranges, and when a `:collection` is given the `:select` input will be
2013-01-28 16:02:59 -05:00
rendered by default, so we don't need to pass the `as: :select` option. Other types of collection
2013-09-21 11:02:47 -04:00
are `:radio_buttons` and `:check_boxes` . Those are added by **Simple Form** to Rails set of form
2013-12-09 12:34:04 -05:00
helpers (read Extra Helpers section below for more information).
2011-09-23 16:00:47 -04:00
2012-01-24 11:15:35 -05:00
Collection inputs accept two other options beside collections:
2011-09-23 16:00:47 -04:00
2013-12-12 11:53:40 -05:00
* *label_method* => the label method to be applied to the collection to retrieve the label (use this
2012-01-27 15:35:43 -05:00
instead of the `text_method` option in `collection_select` )
2011-09-23 16:00:47 -04:00
2013-12-12 11:53:40 -05:00
* *value_method* => the value method to be applied to the collection to retrieve the value
2011-09-23 16:00:47 -04:00
2012-01-27 14:13:35 -05:00
Those methods are useful to manipulate the given collection. Both of these options also accept
lambda/procs in case you want to calculate the value or label in a special way eg. custom
translation. All other options given are sent straight to the underlying helper. For example, you
can give prompt as:
2011-09-23 16:00:47 -04:00
```ruby
2013-01-28 16:02:59 -05:00
f.input :age, collection: 18..60, prompt: "Select your age"
2011-09-23 16:00:47 -04:00
```
2012-01-27 15:35:43 -05:00
It is also possible to create grouped collection selects, that will use the html *optgroup* tags, like this:
2012-01-24 11:15:35 -05:00
```ruby
2013-01-28 16:02:59 -05:00
f.input :country_id, collection: @continents , as: :grouped_select, group_method: :countries
2012-01-24 11:15:35 -05:00
```
2012-01-27 14:13:35 -05:00
Grouped collection inputs accept the same `:label_method` and `:value_method` options, which will be
used to retrieve label/value attributes for the `option` tags. Besides that, you can give:
2012-01-24 11:15:35 -05:00
2013-12-12 11:53:40 -05:00
* *group_method* => the method to be called on the given collection to generate the options for
2012-01-27 15:35:43 -05:00
each group (required)
2012-01-24 11:15:35 -05:00
2013-12-12 11:53:40 -05:00
* *group_label_method* => the label method to be applied on the given collection to retrieve the label
2013-09-21 11:02:47 -04:00
for the _optgroup_ (**Simple Form** will attempt to guess the best one the same way it does with
2012-01-27 14:13:35 -05:00
`:label_method` )
2012-01-24 11:15:35 -05:00
2011-09-23 16:00:47 -04:00
### Priority
2013-09-21 11:02:47 -04:00
**Simple Form** also supports `:time_zone` and `:country` . When using such helpers, you can give
2014-03-14 15:16:50 -04:00
`:priority` as an option to select which time zones and/or countries should be given higher priority:
2011-09-23 16:00:47 -04:00
```ruby
2013-01-28 16:02:59 -05:00
f.input :residence_country, priority: [ "Brazil" ]
f.input :time_zone, priority: /US/
2011-09-23 16:00:47 -04:00
```
2014-03-14 15:16:50 -04:00
Those values can also be configured with a default value to be used on the site through the
2012-01-27 14:13:35 -05:00
`SimpleForm.country_priority` and `SimpleForm.time_zone_priority` helpers.
2011-09-23 16:00:47 -04:00
2012-01-27 15:35:43 -05:00
Note: While using `country_select` if you want to restrict to only a subset of countries for a specific
drop down then you may use the `:collection` option:
2011-12-08 23:24:05 -05:00
```ruby
2013-01-28 16:02:59 -05:00
f.input :shipping_country, priority: [ "Brazil" ], collection: [ "Australia", "Brazil", "New Zealand"]
2011-12-08 23:24:05 -05:00
```
2012-03-03 12:46:38 -05:00
### Associations
2011-09-23 16:00:47 -04:00
2014-03-14 17:24:35 -04:00
To deal with associations, **Simple Form** can generate select inputs, a series of radios buttons or checkboxes.
Lets see how it works: imagine you have a user model that belongs to a company and `has_and_belongs_to_many`
2012-01-27 14:13:35 -05:00
roles. The structure would be something like:
2011-09-23 16:00:47 -04:00
```ruby
2012-01-24 17:10:14 -05:00
class User < ActiveRecord::Base
belongs_to :company
has_and_belongs_to_many :roles
end
2011-09-23 16:00:47 -04:00
2012-01-24 17:10:14 -05:00
class Company < ActiveRecord::Base
has_many :users
end
2011-09-23 16:00:47 -04:00
2012-01-24 17:10:14 -05:00
class Role < ActiveRecord::Base
has_and_belongs_to_many :users
end
2011-09-23 16:00:47 -04:00
```
Now we have the user form:
```erb
2012-01-24 17:10:14 -05:00
< %= simple_form_for @user do |f| %>
< %= f.input :name %>
< %= f.association :company %>
< %= f.association :roles %>
< %= f.button :submit %>
< % end %>
2011-09-23 16:00:47 -04:00
```
2012-01-30 11:48:24 -05:00
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 radio
2013-09-16 08:54:40 -04:00
buttons and checkboxes as well:
2011-09-23 16:00:47 -04:00
```ruby
2013-01-28 16:02:59 -05:00
f.association :company, as: :radio_buttons
f.association :roles, as: :check_boxes
2011-09-23 16:00:47 -04:00
```
2012-01-30 11:48:24 -05:00
The association helper just invokes `input` under the hood, so all options available to `:select` ,
2012-01-27 15:35:43 -05:00
`:radio_buttons` and `:check_boxes` are also available to association. Additionally, you can specify
the collection by hand, all together with the prompt:
2011-09-23 16:00:47 -04:00
```ruby
2013-01-28 16:02:59 -05:00
f.association :company, collection: Company.active.all(order: 'name'), prompt: "Choose a Company"
2011-09-23 16:00:47 -04:00
```
2012-09-17 13:32:32 -04:00
In case you want to declare different labels and values:
```ruby
2013-01-28 16:02:59 -05:00
f.association :company, label_method: :company_name, value_method: :id, include_blank: false
2012-09-17 13:32:32 -04:00
```
2013-09-21 10:58:50 -04:00
Please note that the association helper is currently only tested with Active Record. It currently
does not work well with Mongoid and depending on the ORM you're using your mileage may vary.
2013-07-03 11:11:36 -04:00
2012-03-03 12:46:38 -05:00
### Buttons
2011-09-23 16:00:47 -04:00
2013-09-21 11:02:47 -04:00
All web forms need buttons, right? **Simple Form** wraps them in the DSL, acting like a proxy:
2011-09-23 16:00:47 -04:00
```erb
2012-01-24 17:10:14 -05:00
< %= simple_form_for @user do |f| %>
< %= f.input :name %>
< %= f.button :submit %>
< % end %>
2011-09-23 16:00:47 -04:00
```
The above will simply call submit. You choose to use it or not, it's just a question of taste.
2012-03-03 12:46:38 -05:00
### Wrapping Rails Form Helpers
2011-09-23 16:00:47 -04:00
2013-09-21 11:02:47 -04:00
Say you wanted to use a rails form helper but still wrap it in **Simple Form** goodness? You can, by
2012-01-27 14:13:35 -05:00
calling input with a block like so:
2011-09-23 16:00:47 -04:00
```erb
2012-01-24 17:10:14 -05:00
< %= f.input :role do %>
2013-01-28 16:02:59 -05:00
< %= f.select :role, Role.all.map { |r| [r.name, r.id, { class: r.company.id }] }, include_blank: true %>
2012-01-24 17:10:14 -05:00
< % end %>
2011-09-23 16:00:47 -04:00
```
2012-01-27 14:13:35 -05:00
In the above example, we're taking advantage of Rails 3's select method that allows us to pass in a
hash of additional attributes for each option.
2011-09-23 16:00:47 -04:00
2012-03-03 12:46:38 -05:00
### Extra helpers
2011-09-23 16:00:47 -04:00
2013-09-21 11:02:47 -04:00
**Simple Form** also comes with some extra helpers you can use inside rails default forms without relying
2012-01-27 15:35:43 -05:00
on `simple_form_for` helper. They are listed below.
2011-09-23 16:00:47 -04:00
2012-03-03 12:46:38 -05:00
#### Simple Fields For
2011-09-23 16:00:47 -04:00
2013-09-21 11:02:47 -04:00
Wrapper to use **Simple Form** inside a default rails form. It works in the same way that the `fields_for`
2012-04-22 23:19:08 -04:00
Rails helper, but change the builder to use the `SimpleForm::FormBuilder` .
2011-09-23 16:00:47 -04:00
```ruby
2012-01-24 17:10:14 -05:00
form_for @user do |f|
f.simple_fields_for :posts do |posts_form|
# Here you have all simple_form methods available
posts_form.input :title
2011-09-23 16:00:47 -04:00
end
2012-01-24 17:10:14 -05:00
end
2011-09-23 16:00:47 -04:00
```
2012-03-03 12:46:38 -05:00
#### Collection Radio Buttons
2011-09-23 16:00:47 -04:00
2012-01-27 15:35:43 -05:00
Creates a collection of radio inputs with labels associated (same API as `collection_select` ):
2011-09-23 16:00:47 -04:00
```ruby
2012-01-24 17:10:14 -05:00
form_for @user do |f|
2012-01-27 13:30:19 -05:00
f.collection_radio_buttons :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
2012-01-24 17:10:14 -05:00
end
2011-09-23 16:00:47 -04:00
```
```html
2012-01-24 17:10:14 -05:00
< input id = "user_options_true" name = "user[options]" type = "radio" value = "true" / >
2012-01-27 13:30:19 -05:00
< label class = "collection_radio_buttons" for = "user_options_true" > Yes< / label >
2012-01-24 17:10:14 -05:00
< input id = "user_options_false" name = "user[options]" type = "radio" value = "false" / >
2012-01-27 13:30:19 -05:00
< label class = "collection_radio_buttons" for = "user_options_false" > No< / label >
2011-09-23 16:00:47 -04:00
```
2012-03-03 12:46:38 -05:00
#### Collection Check Boxes
2011-09-23 16:00:47 -04:00
2013-09-16 08:54:40 -04:00
Creates a collection of checkboxes with labels associated (same API as `collection_select` ):
2011-09-23 16:00:47 -04:00
```ruby
2012-01-24 17:10:14 -05:00
form_for @user do |f|
f.collection_check_boxes :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
end
2011-09-23 16:00:47 -04:00
```
```html
2012-01-24 17:10:14 -05:00
< input name = "user[options][]" type = "hidden" value = "" / >
< input id = "user_options_true" name = "user[options][]" type = "checkbox" value = "true" / >
< label class = "collection_check_box" for = "user_options_true" > Yes< / label >
< input name = "user[options][]" type = "hidden" value = "" / >
< input id = "user_options_false" name = "user[options][]" type = "checkbox" value = "false" / >
< label class = "collection_check_box" for = "user_options_false" > No< / label >
2011-09-23 16:00:47 -04:00
```
To use this with associations in your model, you can do the following:
```ruby
2012-01-24 17:10:14 -05:00
form_for @user do |f|
f.collection_check_boxes :role_ids, Role.all, :id, :name # using :roles here is not going to work.
end
2011-09-23 16:00:47 -04:00
```
2013-09-16 08:55:26 -04:00
## Available input types and defaults for each column type
2011-09-23 16:00:47 -04:00
2013-09-22 05:28:23 -04:00
The following table shows the html element you will get for each attribute
according to its database definition. These defaults can be changed by
specifying the helper method in the column `Mapping` as the `as:` option.
2011-09-23 16:00:47 -04:00
2013-10-16 10:42:18 -04:00
Mapping | Generated HTML Element | Database Column Type
--------------- |:-------------------------------------|:--------------------
`boolean` | `input[type=checkbox]` | `boolean`
`string` | `input[type=text]` | `string`
`email` | `input[type=email]` | `string` with `name =~ /email/`
`url` | `input[type=url]` | `string` with `name =~ /url/`
`tel` | `input[type=tel]` | `string` with `name =~ /phone/`
`password` | `input[type=password]` | `string` with `name =~ /password/`
`search` | `input[type=search]` | -
`text` | `textarea` | `text`
`file` | `input[type=file]` | `string` responding to file methods
`hidden` | `input[type=hidden]` | -
`integer` | `input[type=number]` | `integer`
`float` | `input[type=number]` | `float`
`decimal` | `input[type=number]` | `decimal`
`range` | `input[type=range]` | -
2013-11-26 17:00:17 -05:00
`datetime` | `datetime select` | `datetime/timestamp`
`date` | `date select` | `date`
`time` | `time select` | `time`
2013-10-16 10:42:18 -04:00
`select` | `select` | `belongs_to` /`has_many`/`has_and_belongs_to_many` associations
`radio_buttons` | collection of `input[type=radio]` | `belongs_to` associations
`check_boxes` | collection of `input[type=checkbox]` | `has_many` /`has_and_belongs_to_many` associations
`country` | `select` (countries as options) | `string` with `name =~ /country/`
`time_zone` | `select` (timezones as options) | `string` with `name =~ /time_zone/`
2011-09-23 16:00:47 -04:00
## Custom inputs
2013-09-21 11:02:47 -04:00
It is very easy to add custom inputs to **Simple Form** . For instance, if you want to add a custom input
2012-01-27 14:13:35 -05:00
that extends the string one, you just need to add this file:
2011-09-23 16:00:47 -04:00
```ruby
2012-01-24 17:10:14 -05:00
# app/inputs/currency_input.rb
class CurrencyInput < SimpleForm::Inputs::Base
2014-03-14 17:28:26 -04:00
def input(wrapper_options)
2012-01-24 17:10:14 -05:00
"$ #{@builder.text_field(attribute_name, input_html_options)}".html_safe
end
end
2011-09-23 16:00:47 -04:00
```
And use it in your views:
```ruby
2013-01-28 16:02:59 -05:00
f.input :money, as: :currency
2011-09-23 16:00:47 -04:00
```
2013-09-24 21:12:09 -04:00
Note, you may have to create the `app/inputs/` directory and restart your webserver.
2011-09-23 16:00:47 -04:00
2013-09-21 11:02:47 -04:00
You can also redefine existing **Simple Form** inputs by creating a new class with the same name. For
2012-01-27 14:13:35 -05:00
instance, if you want to wrap date/time/datetime in a div, you can do:
2011-09-23 16:00:47 -04:00
```ruby
2012-01-24 17:10:14 -05:00
# app/inputs/date_time_input.rb
class DateTimeInput < SimpleForm::Inputs::DateTimeInput
2014-03-14 17:28:26 -04:00
def input(wrapper_options)
2012-07-06 08:31:29 -04:00
template.content_tag(:div, super)
2012-01-24 17:10:14 -05:00
end
end
2011-09-23 16:00:47 -04:00
```
2012-04-21 22:21:36 -04:00
Or if you want to add a class to all the select fields you can do:
```ruby
# app/inputs/collection_select_input.rb
class CollectionSelectInput < SimpleForm::Inputs::CollectionSelectInput
def input_html_classes
super.push('chosen')
end
end
```
2011-09-23 16:00:47 -04:00
## Custom form builder
2013-09-21 11:02:47 -04:00
You can create a custom form builder that uses **Simple Form** .
2011-09-23 16:00:47 -04:00
2012-01-27 15:35:43 -05:00
Create a helper method that calls `simple_form_for` with a custom builder:
2011-09-23 16:00:47 -04:00
```ruby
2012-01-24 17:10:14 -05:00
def custom_form_for(object, *args, & block)
options = args.extract_options!
2013-01-28 16:02:59 -05:00
simple_form_for(object, *(args < < options.merge ( builder: CustomFormBuilder ) ) , & block )
2012-01-24 17:10:14 -05:00
end
2011-09-23 16:00:47 -04:00
```
2012-01-27 15:35:43 -05:00
Create a form builder class that inherits from `SimpleForm::FormBuilder` .
2011-09-23 16:00:47 -04:00
```ruby
2012-01-24 17:10:14 -05:00
class CustomFormBuilder < SimpleForm::FormBuilder
def input(attribute_name, options = {}, & block)
2013-01-28 16:02:59 -05:00
options[:input_html].merge! class: 'custom'
2012-01-24 17:10:14 -05:00
super
2011-09-23 16:00:47 -04:00
end
2012-01-24 17:10:14 -05:00
end
2011-09-23 16:00:47 -04:00
```
## I18n
2013-09-21 11:02:47 -04:00
**Simple Form** uses all power of I18n API to lookup labels, hints and placeholders. To customize your
2012-01-27 14:13:35 -05:00
forms you can create a locale file like this:
2011-09-23 16:00:47 -04:00
```yaml
2012-01-24 17:10:14 -05:00
en:
simple_form:
labels:
user:
username: 'User name'
password: 'Password'
hints:
user:
username: 'User name to sign in.'
password: 'No special characters, please.'
placeholders:
user:
username: 'Your username'
password: '****'
2011-09-23 16:00:47 -04:00
```
And your forms will use this information to render the components for you.
2013-09-21 11:02:47 -04:00
**Simple Form** also lets you be more specific, separating lookups through actions for labels, hints and
2012-01-27 14:13:35 -05:00
placeholders. Let's say you want a different label for new and edit actions, the locale file would
be something like:
2011-09-23 16:00:47 -04:00
```yaml
2012-01-24 17:10:14 -05:00
en:
simple_form:
labels:
user:
username: 'User name'
password: 'Password'
edit:
username: 'Change user name'
password: 'Change password'
2011-09-23 16:00:47 -04:00
```
2013-09-21 11:02:47 -04:00
This way **Simple Form** will figure out the right translation for you, based on the action being
2012-01-27 14:13:35 -05:00
rendered. And to be a little bit DRYer with your locale file, you can specify defaults for all
models under the 'defaults' key:
2011-09-23 16:00:47 -04:00
```yaml
2012-01-24 17:10:14 -05:00
en:
simple_form:
labels:
defaults:
username: 'User name'
password: 'Password'
new:
username: 'Choose a user name'
hints:
defaults:
username: 'User name to sign in.'
password: 'No special characters, please.'
placeholders:
defaults:
username: 'Your username'
password: '****'
2011-09-23 16:00:47 -04:00
```
2013-09-21 11:02:47 -04:00
**Simple Form** will always look for a default attribute translation under the "defaults" key if no
2012-05-01 23:25:50 -04:00
specific is found inside the model key. Note that this syntax is different from 1.x. To migrate to
2012-01-27 14:13:35 -05:00
the new syntax, just move "labels.#{attribute}" to "labels.defaults.#{attribute}".
2011-09-23 16:00:47 -04:00
2014-03-14 17:28:46 -04:00
In addition, **Simple Form** will fallback to default `human_attribute_name` from Rails when no other
2012-01-27 14:13:35 -05:00
translation is found for labels. Finally, you can also overwrite any label, hint or placeholder
inside your view, just by passing the option manually. This way the I18n lookup will be skipped.
2011-09-23 16:00:47 -04:00
2013-09-21 11:02:47 -04:00
**Simple Form** also has support for translating options in collection helpers. For instance, given a
2012-01-27 14:13:35 -05:00
User with a `:gender` attribute, you might want to create a select box showing translated labels
2013-09-21 11:02:47 -04:00
that would post either `male` or `female` as value. With **Simple Form** you could create an input
2012-01-27 14:13:35 -05:00
like this:
2012-01-24 12:36:40 -05:00
```ruby
2013-01-28 16:02:59 -05:00
f.input :gender, collection: [:male, :female]
2012-01-24 12:36:40 -05:00
```
2013-09-21 11:02:47 -04:00
And **Simple Form** will try a lookup like this in your locale file, to find the right labels to show:
2012-01-24 12:36:40 -05:00
```yaml
2012-01-24 17:10:14 -05:00
en:
simple_form:
options:
user:
gender:
male: 'Male'
2012-08-17 22:33:43 -04:00
female: 'Female'
2012-01-24 12:36:40 -05:00
```
2012-01-27 14:13:35 -05:00
You can also use the `defaults` key as you would do with labels, hints and placeholders. It is
2013-09-21 11:02:47 -04:00
important to notice that **Simple Form** will only do the lookup for options if you give a collection
2012-01-27 14:13:35 -05:00
composed of symbols only. This is to avoid constant lookups to I18n.
2012-01-24 12:36:40 -05:00
2011-09-23 16:00:47 -04:00
It's also possible to translate buttons, using Rails' built-in I18n support:
```yaml
2012-01-24 17:10:14 -05:00
en:
helpers:
submit:
user:
create: "Add %{model}"
update: "Save Changes"
2011-09-23 16:00:47 -04:00
```
2012-01-27 14:13:35 -05:00
There are other options that can be configured through I18n API, such as required text and boolean.
Be sure to check our locale file or the one copied to your application after you run
2012-01-27 15:35:43 -05:00
`rails generate simple_form:install` .
2011-09-23 16:00:47 -04:00
2012-05-01 23:25:50 -04:00
It should be noted that translations for labels, hints and placeholders for a namespaced model, e.g.
`Admin::User` , should be placed under `admin_user` , not under `admin/user` . This is different from
how translations for namespaced model and attribute names are defined:
```yaml
en:
activerecord:
models:
admin/user: User
attributes:
admin/user:
name: Name
```
They should be placed under `admin/user` . Form labels, hints and placeholders for those attributes,
though, should be placed under `admin_user` :
```yaml
en:
simple_form:
labels:
admin_user:
name: Name
```
2013-09-21 11:02:47 -04:00
This difference exists because **Simple Form** relies on `object_name` provided by Rails'
2012-05-02 01:04:35 -04:00
FormBuilder to determine the translation path for a given object instead of `i18n_key` from the
object itself. Thus, similarly, if a form for an `Admin::User` object is defined by calling
2013-09-21 11:02:47 -04:00
`simple_form_for @admin_user, as: :some_user` , **Simple Form** will look for translations
2012-05-02 01:04:35 -04:00
under `some_user` instead of `admin_user` .
2012-03-03 12:46:38 -05:00
## Configuration
2013-09-21 11:02:47 -04:00
**Simple Form** has several configuration options. You can read and change them in the initializer
created by **Simple Form** , so if you haven't executed the command below yet, please do:
2012-03-03 12:46:38 -05:00
`rails generate simple_form:install`
### The wrappers API
2013-09-21 11:02:47 -04:00
With **Simple Form** you can configure how your components will be rendered using the wrappers API.
2012-03-03 12:46:38 -05:00
The syntax looks like this:
```ruby
2013-01-28 16:02:59 -05:00
config.wrappers tag: :div, class: :input,
error_class: :field_with_errors do |b|
2012-03-03 12:46:38 -05:00
# Form extensions
b.use :html5
b.optional :pattern
b.use :maxlength
b.use :placeholder
b.use :readonly
# Form components
b.use :label_input
2013-01-28 16:02:59 -05:00
b.use :hint, wrap_with: { tag: :span, class: :hint }
b.use :error, wrap_with: { tag: :span, class: :error }
2012-03-03 12:46:38 -05:00
end
```
2013-09-21 10:58:50 -04:00
The _Form components_ will generate the form tags like labels, inputs, hints or errors contents.
The available components are:
2012-04-09 15:07:31 -04:00
```ruby
:label # The < label > tag alone
:input # The < input > tag alone
:label_input # The < label > and the < input > tags
:hint # The hint for the input
:error # The error for the input
```
2012-03-03 12:46:38 -05:00
The _Form extensions_ are used to generate some attributes or perform some lookups on the model to
add extra information to your components.
You can create new _Form components_ using the wrappers API as in the following example:
```ruby
config.wrappers do |b|
b.use :placeholder
b.use :label_input
2013-01-28 16:02:59 -05:00
b.wrapper tag: :div, class: 'separator' do |component|
component.use :hint, wrap_with: { tag: :span, class: :hint }
component.use :error, wrap_with: { tag: :span, class: :error }
2012-03-03 12:46:38 -05:00
end
end
```
this will wrap the hint and error components within a `div` tag using the class `'separator'` .
2014-03-14 17:50:00 -04:00
You can customize _Form components_ passing options to them:
```ruby
config.wrappers do |b|
b.use :label_input, class: 'label-input-class'
end
```
This you set the input and label class to `'label-input-class'` .
2012-03-03 12:46:38 -05:00
If you want to customize the custom _Form components_ on demand you can give it a name like this:
```ruby
config.wrappers do |b|
b.use :placeholder
b.use :label_input
2014-03-12 18:11:33 -04:00
b.wrapper :my_wrapper, tag: :div, class: 'separator', html: { id: 'my_wrapper_id' } do |component|
2013-01-28 16:02:59 -05:00
component.use :hint, wrap_with: { tag: :span, class: :hint }
component.use :error, wrap_with: { tag: :span, class: :error }
2012-03-03 12:46:38 -05:00
end
end
```
and now you can pass options to your `input` calls to customize the `:my_wrapper` _Form component_ .
```ruby
# Completely turns off the custom wrapper
2013-01-28 16:02:59 -05:00
f.input :name, my_wrapper: false
2012-03-03 12:46:38 -05:00
# Configure the html
2013-01-28 16:02:59 -05:00
f.input :name, my_wrapper_html: { id: 'special_id' }
2012-03-03 12:46:38 -05:00
# Configure the tag
2013-01-28 16:02:59 -05:00
f.input :name, my_wrapper_tag: :p
2012-03-03 12:46:38 -05:00
```
You can also define more than one wrapper and pick one to render in a specific form or input.
To define another wrapper you have to give it a name, as the follow:
```ruby
config.wrappers :small do |b|
b.use :placeholder
b.use :label_input
end
```
and use it in this way:
```ruby
# Specifying to whole form
2013-01-28 16:02:59 -05:00
simple_form_for @user , wrapper: :small do |f|
2012-03-03 12:46:38 -05:00
f.input :name
end
# Specifying to one input
simple_form_for @user do |f|
2013-01-28 16:02:59 -05:00
f.input :name, wrapper: :small
2012-03-03 12:46:38 -05:00
end
```
2013-09-21 11:02:47 -04:00
**Simple Form** also allows you to use optional elements. For instance, let's suppose you want to use
2012-03-03 12:46:38 -05:00
hints or placeholders, but you don't want them to be generated automatically. You can set their
2013-03-08 20:03:52 -05:00
default values to `false` or use the `optional` method. Is preferable to use the `optional` syntax:
2012-03-03 12:46:38 -05:00
```ruby
2013-01-28 16:02:59 -05:00
config.wrappers placeholder: false do |b|
2012-03-03 12:46:38 -05:00
b.use :placeholder
b.use :label_input
2013-01-28 16:02:59 -05:00
b.wrapper tag: :div, class: 'separator' do |component|
component.optional :hint, wrap_with: { tag: :span, class: :hint }
component.use :error, wrap_with: { tag: :span, class: :error }
2012-03-03 12:46:38 -05:00
end
end
```
2013-01-28 16:02:59 -05:00
By setting it as `optional` , a hint will only be generated when `hint: true` is explicitly used.
2013-03-08 20:03:52 -05:00
The same for placeholder.
2012-03-03 12:46:38 -05:00
2011-09-23 16:00:47 -04:00
## HTML 5 Notice
2013-09-21 11:02:47 -04:00
By default, **Simple Form** will generate input field types and attributes that are supported in HTML5,
2012-01-27 14:13:35 -05:00
but are considered invalid HTML for older document types such as HTML4 or XHTML1.0. The HTML5
extensions include the new field types such as email, number, search, url, tel, and the new
attributes such as required, autofocus, maxlength, min, max, step.
2011-09-23 16:00:47 -04:00
2012-01-27 14:13:35 -05:00
Most browsers will not care, but some of the newer ones - in particular Chrome 10+ - use the
required attribute to force a value into an input and will prevent form submission without it.
Depending on the design of the application this may or may not be desired. In many cases it can
break existing UI's.
2011-09-23 16:00:47 -04:00
2013-09-21 11:02:47 -04:00
It is possible to disable all HTML 5 extensions in **Simple Form** with the following configuration:
2011-09-23 16:00:47 -04:00
```ruby
2012-01-24 17:10:14 -05:00
SimpleForm.html5 = false # default is true
2011-09-23 16:00:47 -04:00
```
2012-01-27 14:13:35 -05:00
If you want to have all other HTML 5 features, such as the new field types, you can disable only
the browser validation:
2011-09-23 16:00:47 -04:00
```ruby
2012-01-24 17:10:14 -05:00
SimpleForm.browser_validations = false # default is true
2011-09-23 16:00:47 -04:00
```
2012-01-27 14:13:35 -05:00
This option adds a new `novalidate` property to the form, instructing it to skip all HTML 5
validation. The inputs will still be generated with the required and other attributes, that might
help you to use some generic javascript validation.
2011-09-23 16:00:47 -04:00
You can also add `novalidate` to a specific form by setting the option on the form itself:
```erb
2014-03-14 17:28:46 -04:00
< %= simple_form_for(resource, html: { novalidate: true }) do |form| %>
2011-09-23 16:00:47 -04:00
```
2013-07-17 15:20:20 -04:00
Please notice that any of the configurations above will not disable the `placeholder` component,
2012-01-27 14:13:35 -05:00
which is an HTML 5 feature. We believe most of the newest browsers are handling this attribute fine,
and if they aren't, any plugin you use would take of using the placeholder attribute to do it.
However, you can disable it if you want, by removing the placeholder component from the components
2013-09-21 11:02:47 -04:00
list in **Simple Form** configuration file.
2011-09-23 16:00:47 -04:00
2012-01-30 07:59:07 -05:00
## Information
2011-09-23 16:00:47 -04:00
2012-01-30 07:59:07 -05:00
### Google Group
If you have any questions, comments, or concerns please use the Google Group instead of the GitHub
Issues tracker:
http://groups.google.com/group/plataformatec-simpleform
### RDocs
2013-09-21 11:02:47 -04:00
You can view the **Simple Form** documentation in RDoc format here:
2012-01-30 07:59:07 -05:00
http://rubydoc.info/github/plataformatec/simple_form/master/frames
### Bug reports
If you discover any bugs, feel free to create an issue on GitHub. Please add as much information as
possible to help us fixing the possible bug. We also encourage you to help even more by forking and
sending us a pull request.
https://github.com/plataformatec/simple_form/issues
2011-09-23 16:00:47 -04:00
## Maintainers
2012-01-27 14:16:42 -05:00
* José Valim (https://github.com/josevalim)
2011-09-23 16:00:47 -04:00
* Carlos Antonio da Silva (https://github.com/carlosantoniodasilva)
* Rafael Mendonça França (https://github.com/rafaelfranca)
2012-08-21 13:28:16 -04:00
* Vasiliy Ermolovich (https://github.com/nashby)
2011-09-23 16:00:47 -04:00
## License
2014-01-03 08:29:52 -05:00
MIT License. Copyright 2009-2014 Plataformatec. http://plataformatec.com.br
2013-02-26 13:34:19 -05:00
2013-09-21 10:58:50 -04:00
You are not granted rights or licenses to the trademarks of the Plataformatec, including without
limitation the Simple Form name or logo.