README.md in simple_form-2.1.3 vs README.md in simple_form-3.0.0.beta1
- old
+ new
@@ -4,12 +4,16 @@
**SimpleForm** aims to be as flexible as possible while helping you with powerful components to create
your forms. The basic goal of SimpleForm is to not touch your way of defining the layout, letting
you find the better design for your eyes. Most of the DSL was inherited from Formtastic,
which we are thankful for and should make you feel right at home.
-INFO: This README refers to **SimpleForm** 2.1. For older releases, check the related branch for your version.
+INFO: This README is [also available in a friendly navigable format](http://simple-form.plataformatec.com.br/)
+and refers to **SimpleForm** 2.0. If you are using **SimpleForm** in the versions 1.x, you should
+check this branch:
+https://github.com/plataformatec/simple_form/tree/v1.5
+
## Installation
Add it to your Gemfile:
```ruby
@@ -33,27 +37,27 @@
```ruby
gem 'country_select'
```
-### Bootstrap
+### Twitter Bootstrap
-**Simple Form** can be easily integrated to the [Bootstrap](http://getbootstrap.com/2.3.2/) 2.3.
+**SimpleForm** 2.0 can be easily integrated to the [Twitter Bootstrap](http://twitter.github.com/bootstrap).
To do that you have to use the `bootstrap` option in the install generator, like this:
```console
rails generate simple_form:install --bootstrap
```
-You have to be sure that you added a copy of the [Bootstrap](http://getbootstrap.com/2.3.2/)
+You have to be sure that you added a copy of the [Twitter Bootstrap](http://twitter.github.com/bootstrap)
assets on your application.
For more information see the generator output, our
[example application code](https://github.com/rafaelfranca/simple_form-bootstrap) and
[the live example app](http://simple-form-bootstrap.plataformatec.com.br/).
-**NOTE**: **SimpleForm** integration requires Twitter Bootstrap version 2.0 up to 2.3.
+**NOTE**: **SimpleForm** integration requires Twitter Bootstrap version 2.0 or higher.
### Zurb Foundation 3
To generate wrappers that are compatible with [Zurb Foundation 3](http://foundation.zurb.com/), pass the `foundation` option to the generator, like this:
@@ -89,63 +93,63 @@
You can overwrite the default label by passing it to the input method. You can also add a hint or
even a placeholder. For boolean inputs, you can add an inline label as well:
```erb
<%= simple_form_for @user do |f| %>
- <%= 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' %>
+ <%= 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' %>
<%= f.button :submit %>
<% end %>
```
In some cases you may want to disable labels, hints or error. Or you may want to configure the html
of any of them:
```erb
<%= simple_form_for @user do |f| %>
- <%= f.input :username, :label_html => { :class => 'my_class' } %>
- <%= f.input :password, :hint => false, :error_html => { :id => 'password_error'} %>
- <%= f.input :password_confirmation, :label => false %>
+ <%= f.input :username, label_html: { class: 'my_class' } %>
+ <%= f.input :password, hint: false, error_html: { id: 'password_error'} %>
+ <%= f.input :password_confirmation, label: false %>
<%= f.button :submit %>
<% end %>
```
It is also possible to pass any html attribute straight to the input, by using the `:input_html`
option, for instance:
```erb
<%= simple_form_for @user do |f| %>
- <%= f.input :username, :input_html => { :class => 'special' } %>
- <%= f.input :password, :input_html => { :maxlength => 20 } %>
- <%= f.input :remember_me, :input_html => { :value => '1' } %>
+ <%= f.input :username, input_html: { class: 'special' } %>
+ <%= f.input :password, input_html: { maxlength: 20 } %>
+ <%= f.input :remember_me, input_html: { value: '1' } %>
<%= f.button :submit %>
<% end %>
```
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
overwrite the defaults:
```erb
-<%= 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' } %>
+<%= 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' } %>
<%= f.button :submit %>
<% end %>
```
Since **SimpleForm** generates a wrapper div around your label and input by default, you can pass
any html attribute to that wrapper as well using the `:wrapper_html` option, like so:
```erb
<%= simple_form_for @user do |f| %>
- <%= f.input :username, :wrapper_html => { :class => 'username' } %>
- <%= f.input :password, :wrapper_html => { :id => 'password' } %>
- <%= f.input :remember_me, :wrapper_html => { :class => 'options' } %>
+ <%= f.input :username, wrapper_html: { class: 'username' } %>
+ <%= f.input :password, wrapper_html: { id: 'password' } %>
+ <%= f.input :remember_me, wrapper_html: { class: 'options' } %>
<%= f.button :submit %>
<% end %>
```
Required fields are marked with an * prepended to their labels.
@@ -154,58 +158,51 @@
And of course, the `required` property of any input can be overwritten as needed:
```erb
<%= simple_form_for @user do |f| %>
- <%= f.input :name, :required => false %>
+ <%= f.input :name, required: false %>
<%= f.input :username %>
<%= f.input :password %>
<%= f.button :submit %>
<% end %>
```
-By default, SimpleForm will look at the column type in the database and use an
-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.
-
**SimpleForm** also lets you overwrite the default input type it creates:
```erb
<%= simple_form_for @user do |f| %>
<%= f.input :username %>
<%= f.input :password %>
- <%= f.input :description, :as => :text %>
- <%= f.input :accepts, :as => :radio_buttons %>
+ <%= f.input :description, as: :text %>
+ <%= f.input :accepts, as: :radio_buttons %>
<%= f.button :submit %>
<% end %>
```
So instead of a checkbox for the *accepts* attribute, you'll have a pair of radio buttons with yes/no
-labels and a textarea instead of a text field for the description. You can also render boolean
-attributes using `:as => :select` to show a dropdown.
+labels and a text area instead of a text field for the description. You can also render boolean
+attributes using `as: :select` to show a dropdown.
It is also possible to give the `:disabled` option to **SimpleForm**, and it'll automatically mark
the wrapper as disabled with a css class, so you can style labels, hints and other components inside
the wrapper as well:
```erb
<%= simple_form_for @user do |f| %>
- <%= f.input :username, :disabled => true, :hint => 'You cannot change your username.' %>
+ <%= f.input :username, disabled: true, hint: 'You cannot change your username.' %>
<%= f.button :submit %>
<% end %>
```
**SimpleForm** accepts same options as their corresponding input type helper in Rails:
```erb
<%= simple_form_for @user do |f| %>
- <%= 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] %>
+ <%= 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] %>
<%= f.button :submit %>
<% end %>
```
**SimpleForm** also allows you to use label, hint, input_field, error and full_error helpers
@@ -214,33 +211,55 @@
```erb
<%= simple_form_for @user do |f| %>
<%= f.label :username %>
<%= f.input_field :username %>
<%= f.hint 'No special characters, please!' %>
- <%= f.error :username, :id => 'user_name_error' %>
+ <%= f.error :username, id: 'user_name_error' %>
<%= f.full_error :token %>
<%= f.submit 'Save' %>
<% end %>
```
Any extra option passed to these methods will be rendered as html option.
+### Stripping away all wrapper divs
+
+**SimpleForm** also allows you to strip away all the div wrappers around the `<input>` field that is generated with the usual `f.input`.
+The easiest way to achieve this is to use `f.input_field`.
+
+Example:
+
+```erb
+simple_form_for @user do |f|
+ f.input_field :name
+end
+```
+
+Produces:
+
+```erb
+<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
+
### Collections
And 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:
```erb
<%= simple_form_for @user do |f| %>
<%= f.input :user %>
- <%= f.input :age, :collection => 18..60 %>
+ <%= f.input :age, collection: 18..60 %>
<%= f.button :submit %>
<% end %>
```
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. Other types of collection
+rendered by default, so we don't need to pass the `as: :select` option. Other types of collection
are `:radio_buttons` and `:check_boxes`. Those are added by **SimpleForm** to Rails set of form
helpers (read Extra Helpers session below for more information).
Collection inputs accept two other options beside collections:
@@ -253,17 +272,17 @@
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:
```ruby
-f.input :age, :collection => 18..60, :prompt => "Select your age"
+f.input :age, collection: 18..60, prompt: "Select your age"
```
It is also possible to create grouped collection selects, that will use the html *optgroup* tags, like this:
```ruby
-f.input :country_id, :collection => @continents, :as => :grouped_select, :group_method => :countries
+f.input :country_id, collection: @continents, as: :grouped_select, group_method: :countries
```
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:
@@ -278,27 +297,27 @@
**SimpleForm** also supports `:time_zone` and `:country`. When using such helpers, you can give
`:priority` as option to select which time zones and/or countries should be given higher priority:
```ruby
-f.input :residence_country, :priority => [ "Brazil" ]
-f.input :time_zone, :priority => /US/
+f.input :residence_country, priority: [ "Brazil" ]
+f.input :time_zone, priority: /US/
```
Those values can also be configured with a default value to be used site use through the
`SimpleForm.country_priority` and `SimpleForm.time_zone_priority` helpers.
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:
```ruby
-f.input :shipping_country, :priority => [ "Brazil" ], :collection => [ "Australia", "Brazil", "New Zealand"]
+f.input :shipping_country, priority: [ "Brazil" ], collection: [ "Australia", "Brazil", "New Zealand"]
```
### Associations
-To deal with associations, **SimpleForm** can generate select inputs, a series of radios buttons or checkboxes.
+To deal with associations, **SimpleForm** can generate select inputs, a series of radios buttons or check boxes.
Lets see how it works: imagine you have a user model that belongs to a company and has_and_belongs_to_many
roles. The structure would be something like:
```ruby
class User < ActiveRecord::Base
@@ -326,29 +345,29 @@
<% 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 radio
-buttons and checkboxes as well:
+buttons and check boxes as well:
```ruby
-f.association :company, :as => :radio_buttons
-f.association :roles, :as => :check_boxes
+f.association :company, as: :radio_buttons
+f.association :roles, as: :check_boxes
```
The association helper just invokes `input` under the hood, so all options available to `:select`,
`:radio_buttons` and `:check_boxes` are also available to association. Additionally, you can specify
the collection by hand, all together with the prompt:
```ruby
-f.association :company, :collection => Company.active.all(:order => 'name'), :prompt => "Choose a Company"
+f.association :company, collection: Company.active.all(order: 'name'), prompt: "Choose a Company"
```
In case you want to declare different labels and values:
```ruby
-f.association :company, :label_method => :company_name, :value_method => :id, :include_blank => false
+f.association :company, label_method: :company_name, value_method: :id, include_blank: false
```
### Buttons
All web forms need buttons, right? **SimpleForm** wraps them in the DSL, acting like a proxy:
@@ -367,11 +386,11 @@
Say you wanted to use a rails form helper but still wrap it in **SimpleForm** goodness? You can, by
calling input with a block like so:
```erb
<%= f.input :role do %>
- <%= f.select :role, Role.all.map { |r| [r.name, r.id, { :class => r.company.id }] }, :include_blank => true %>
+ <%= f.select :role, Role.all.map { |r| [r.name, r.id, { class: r.company.id }] }, include_blank: true %>
<% end %>
```
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.
@@ -413,11 +432,11 @@
<label class="collection_radio_buttons" for="user_options_false">No</label>
```
#### Collection Check Boxes
-Creates a collection of checkboxes with labels associated (same API as `collection_select`):
+Creates a collection of check boxes with labels associated (same API as `collection_select`):
```ruby
form_for @user do |f|
f.collection_check_boxes :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
end
@@ -438,41 +457,41 @@
form_for @user do |f|
f.collection_check_boxes :role_ids, Role.all, :id, :name # using :roles here is not going to work.
end
```
-## Available input types and defaults for each column type
+## Mappings/Inputs available
-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.
+**SimpleForm** comes with a lot of default mappings:
- 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]` | -
- `datetime` | `select` | `datetime/timestamp`
- `date` | `select` | `date`
- `time` | `select` | `time`
- `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/`
+```text
+Mapping Input Column Type
+boolean check box boolean
+string text field string
+email email field string with name matching "email"
+url url field string with name matching "url"
+tel tel field string with name matching "phone"
+password password field string with name matching "password"
+search search -
+text text area text
+file file field string, responding to file methods
+hidden hidden field -
+integer number field integer
+float number field float
+decimal number field decimal
+range range field -
+datetime datetime select datetime/timestamp
+date date select date
+time time select time
+select collection select belongs_to/has_many/has_and_belongs_to_many associations
+radio_buttons collection radio buttons belongs_to
+check_boxes collection check boxes has_many/has_and_belongs_to_many associations
+country country select string with name matching "country"
+time_zone time zone select string with name matching "time_zone"
+```
+
## Custom inputs
It is very easy to add custom inputs to **SimpleForm**. For instance, if you want to add a custom input
that extends the string one, you just need to add this file:
@@ -486,13 +505,12 @@
```
And use it in your views:
```ruby
-f.input :money, :as => :currency
+f.input :money, as: :currency
```
-Note, you may have to create the `app/inputs/` directory and restart your webserver.
You can also redefine existing **SimpleForm** inputs by creating a new class with the same name. For
instance, if you want to wrap date/time/datetime in a div, you can do:
```ruby
@@ -522,20 +540,20 @@
Create a helper method that calls `simple_form_for` with a custom builder:
```ruby
def custom_form_for(object, *args, &block)
options = args.extract_options!
- simple_form_for(object, *(args << options.merge(:builder => CustomFormBuilder)), &block)
+ simple_form_for(object, *(args << options.merge(builder: CustomFormBuilder)), &block)
end
```
Create a form builder class that inherits from `SimpleForm::FormBuilder`.
```ruby
class CustomFormBuilder < SimpleForm::FormBuilder
def input(attribute_name, options = {}, &block)
- options[:input_html].merge! :class => 'custom'
+ options[:input_html].merge! class: 'custom'
super
end
end
```
@@ -614,11 +632,11 @@
User with a `:gender` attribute, you might want to create a select box showing translated labels
that would post either `male` or `female` as value. With **SimpleForm** you could create an input
like this:
```ruby
-f.input :gender, :collection => [:male, :female]
+f.input :gender, collection: [:male, :female]
```
And **SimpleForm** will try a lookup like this in your locale file, to find the right labels to show:
```yaml
@@ -676,11 +694,11 @@
```
This difference exists because **SimpleForm** relies on `object_name` provided by Rails'
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
-`simple_form_for @admin_user, :as => :some_user`, **SimpleForm** will look for translations
+`simple_form_for @admin_user, as: :some_user`, **SimpleForm** will look for translations
under `some_user` instead of `admin_user`.
## Configuration
**SimpleForm** has several configuration options. You can read and change them in the initializer
@@ -692,24 +710,24 @@
With **SimpleForm** you can configure how your components will be rendered using the wrappers API.
The syntax looks like this:
```ruby
-config.wrappers :tag => :div, :class => :input,
- :error_class => :field_with_errors do |b|
+config.wrappers tag: :div, class: :input,
+ error_class: :field_with_errors do |b|
# Form extensions
b.use :html5
b.optional :pattern
b.use :maxlength
b.use :placeholder
b.use :readonly
# Form components
b.use :label_input
- b.use :hint, :wrap_with => { :tag => :span, :class => :hint }
- b.use :error, :wrap_with => { :tag => :span, :class => :error }
+ b.use :hint, wrap_with: { tag: :span, class: :hint }
+ b.use :error, wrap_with: { tag: :span, class: :error }
end
```
The _Form components_ will generate the form tags like labels, inputs, hints or errors contents. The available components are:
@@ -728,13 +746,13 @@
```ruby
config.wrappers do |b|
b.use :placeholder
b.use :label_input
- 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 }
+ 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 }
end
end
```
this will wrap the hint and error components within a `div` tag using the class `'separator'`.
@@ -743,28 +761,28 @@
```ruby
config.wrappers do |b|
b.use :placeholder
b.use :label_input
- b.wrapper :my_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 }
+ b.wrapper :my_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 }
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
-f.input :name, :my_wrapper => false
+f.input :name, my_wrapper: false
# Configure the html
-f.input :name, :my_wrapper_html => { :id => 'special_id' }
+f.input :name, my_wrapper_html: { id: 'special_id' }
# Configure the tag
-f.input :name, :my_wrapper_tag => :p
+f.input :name, my_wrapper_tag: :p
```
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:
@@ -777,36 +795,36 @@
and use it in this way:
```ruby
# Specifying to whole form
-simple_form_for @user, :wrapper => :small do |f|
+simple_form_for @user, wrapper: :small do |f|
f.input :name
end
# Specifying to one input
simple_form_for @user do |f|
- f.input :name, :wrapper => :small
+ f.input :name, wrapper: :small
end
```
**SimpleForm** also allows you to use optional elements. For instance, let's suppose you want to use
hints or placeholders, but you don't want them to be generated automatically. You can set their
default values to `false` or use the `optional` method. Is preferible to use the `optional` syntax:
```ruby
-config.wrappers :placeholder => false do |b|
+config.wrappers placeholder: false do |b|
b.use :placeholder
b.use :label_input
- 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 }
+ 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 }
end
end
```
-By setting it as `optional`, a hint will only be generated when `:hint => true` is explicitly used.
+By setting it as `optional`, a hint will only be generated when `hint: true` is explicitly used.
The same for placehold.
## HTML 5 Notice
By default, **SimpleForm** will generate input field types and attributes that are supported in HTML5,
@@ -837,10 +855,10 @@
help you to use some generic javascript validation.
You can also add `novalidate` to a specific form by setting the option on the form itself:
```erb
-<%= simple_form_for(resource, :html => {:novalidate => true}) do |form| %>
+<%= simple_form_for(resource, html: {novalidate: true}) do |form| %>
```
Please notice that any of the configurations above will disable the `placeholder` component,
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.