Review README format

This commit is contained in:
Rafael Mendonça França 2012-01-27 17:13:35 -02:00
parent 16d7b3e93c
commit 10bd50dcf8
1 changed files with 146 additions and 67 deletions

213
README.md
View File

@ -1,13 +1,17 @@
# SimpleForm - Rails forms made easy.
[![Build Status](https://secure.travis-ci.org/plataformatec/simple_form.png)](http://travis-ci.org/plataformatec/simple_form)
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, letting you find the better design for your eyes. Good part of the DSL was inherited from Formtastic, which we are thankful for and should make you feel right at home.
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, letting
you find the better design for your eyes. Good part of the DSL was inherited from Formtastic,
which we are thankful for and should make you feel right at home.
## Information
### Google Group
If you have any questions, comments, or concerns please use the Google Group instead of the GitHub Issues tracker:
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
@ -17,11 +21,14 @@ You can view the SimpleForm documentation in RDoc format here:
http://rubydoc.info/github/plataformatec/simple_form/master/frames
If you need to use SimpleForm with Rails 2.3, you can always run `gem server` from the command line after you install the gem to access the old documentation.
If you need to use SimpleForm with Rails 2.3, you can always run `gem server` from the command line
after you install the gem to access the old documentation.
### 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.
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.
http://github.com/plataformatec/simple_form/issues
@ -39,7 +46,8 @@ Run the generator:
`rails generate simple_form:install`
Also, if you want to use the country select, you will need the country_select plugin, install with following command:
Also, if you want to use the country select, you will need the country_select plugin, install with
following command:
`rails plugin install git://github.com/rails/country_select.git`
@ -50,7 +58,11 @@ if you want to use it with Rails 2.3 you should check this branch:
## Usage
SimpleForm was designed to be customized as you need to. Basically it's a stack of components that 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 form helpers, as they do a great work by themselves. Instead, SimpleForm acts as a DSL and just maps your input type (retrieved from the column definition in the database) to an specific helper method.
SimpleForm was designed to be customized as you need to. Basically it's a stack of components that
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
form helpers, as they do a great work by themselves. Instead, SimpleForm acts as a DSL and just
maps your input type (retrieved from the column definition in the database) to an specific helper method.
To start using SimpleForm you just have to use the helper it provides:
@ -62,7 +74,8 @@ To start using SimpleForm you just have to use the helper it provides:
<% end %>
```
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).
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).
You can overwrite the default label by passing it to the input method, add a hint or even a placeholder:
@ -97,7 +110,9 @@ It is also possible to pass any html attribute straight to the input, by using t
<% end %>
```
If you want to pass the same :input_html 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_html will overwrite the defaults:
If you want to pass the same :input_html 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_html will
overwrite the defaults:
```erb
<%= simple_form_for @user, :defaults => { :class => 'default_class' } do |f| %>
@ -108,7 +123,8 @@ If you want to pass the same :input_html to all inputs in the form (for example,
<% end %>
```
Since simple_form 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:
Since simple_form 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| %>
@ -119,7 +135,8 @@ Since simple_form generates a wrapper div around your label and input by default
<% end %>
```
By default all inputs are required, which means an * is prepended to the label, but you can disable it in any input you want:
By default all inputs are required, which means an * is prepended to the label, but you can disable
it in any input you want:
```erb
<%= simple_form_for @user do |f| %>
@ -142,9 +159,13 @@ SimpleForm also lets you overwrite the default input type it creates:
<% end %>
```
So instead of a checkbox for the :accepts attribute, you'll have a pair of radio buttons with yes/no 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.
So instead of a checkbox for the :accepts attribute, you'll have a pair of radio buttons with yes/no
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:
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| %>
@ -164,7 +185,8 @@ SimpleForm accepts same options as their corresponding input type helper in Rail
<% end %>
```
SimpleForm also allows you to use label, hint, input_field, error and full_error helpers it provides (please take a look at the rdocs for each method for more info):
SimpleForm also allows you to use label, hint, input_field, error and full_error helpers it provides
(please take a look at the rdocs for each method for more info):
```erb
<%= simple_form_for @user do |f| %>
@ -181,7 +203,8 @@ Any extra option passed to these methods will be rendered as html option.
### 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:
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| %>
@ -191,15 +214,22 @@ And what if you want to create a select containing the age from 18 to 60 in your
<% 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 are :radio and :check_boxes. Those are added by SimpleForm to Rails set of form helpers (read Extra Helpers session below for more information).
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
are :radio 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:
* label_method => the label method to be applied to the collection to retrieve the label (use this instead of the text_method option in collection_select)
* label_method => the label method to be applied to the collection to retrieve the label (use this
instead of the text_method option in collection_select)
* value_method => the value method to be applied to the collection to retrieve the value
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:
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:
```ruby
f.input :age, :collection => 18..60, :prompt => "Select your age"
@ -211,24 +241,30 @@ It is also possible to create grouped collection selects, that will use the html
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:
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:
* group_method => the method to be called on the given collection to generate the options for each group (required)
* group_label_method => the label method to be applied on the given collection to retrieve the label for the `optgroup` (SimpleForm will attempt to guess the best one the same way it does with `:label_method`)
* group_label_method => the label method to be applied on the given collection to retrieve the label
for the `optgroup` (SimpleForm will attempt to guess the best one the same way it does with
`:label_method`)
### Priority
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:
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/
```
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.
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:
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"]
@ -255,7 +291,9 @@ And now, you don't need to wrap your f.input calls anymore:
## Associations
To deal with associations, SimpleForm can generate select inputs, a series of radios 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:
To deal with associations, SimpleForm can generate select inputs, a series of radios 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
@ -283,14 +321,18 @@ Now we have the user form:
<% 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:
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:
```ruby
f.association :company, :as => :radio
f.association :roles, :as => :check_boxes
```
The association helper just invokes input under the hood, so all options available to :select, :radio and :check_boxes are also available to association. Additionally, you can specify the collection by hand, all together with the prompt:
The association helper just invokes input under the hood, so all options available to :select, :radio
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"
@ -311,7 +353,8 @@ The above will simply call submit. You choose to use it or not, it's just a ques
## Wrapping Rails Form Helpers
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:
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 %>
@ -319,11 +362,13 @@ Say you wanted to use a rails form helper but still wrap it in SimpleForm goodne
<% 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.
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.
## Extra helpers
SimpleForm also comes with some extra helpers you can use inside rails default forms without relying on simple_form_for helper. They are listed below.
SimpleForm also comes with some extra helpers you can use inside rails default forms without relying
on simple_form_for helper. They are listed below.
### Simple Fields For
@ -386,34 +431,37 @@ end
SimpleForm comes with a lot of default mappings:
Mapping Input Column Type
```
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 collection radio belongs_to
check_boxes collection check box 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"
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 collection radio belongs_to
check_boxes collection check box 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:
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:
```ruby
# app/inputs/currency_input.rb
@ -430,7 +478,8 @@ And use it in your views:
f.input :money, :as => :currency
```
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:
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
# app/inputs/date_time_input.rb
@ -467,7 +516,8 @@ end
## I18n
SimpleForm uses all power of I18n API to lookup labels, hints and placeholders. To customize your forms you can create a locale file like this:
SimpleForm uses all power of I18n API to lookup labels, hints and placeholders. To customize your
forms you can create a locale file like this:
```yaml
en:
@ -488,7 +538,9 @@ en:
And your forms will use this information to render the components for you.
SimpleForm also lets you be more specific, separating lookups through actions for labels, hints and placeholders. Let's say you want a different label for new and edit actions, the locale file would be something like:
SimpleForm also lets you be more specific, separating lookups through actions for labels, hints and
placeholders. Let's say you want a different label for new and edit actions, the locale file would
be something like:
```yaml
en:
@ -502,7 +554,9 @@ en:
password: 'Change 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 specify defaults for all models under the 'defaults' key:
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 specify defaults for all
models under the 'defaults' key:
```yaml
en:
@ -523,11 +577,18 @@ en:
password: '****'
```
SimpleForm will always look for a default attribute translation under the "defaults" key if no specific is found inside the model key.Note that this syntax is different from 1.x. To migrate to the new syntax, just move "labels.#{attribute}" to "labels.defaults.#{attribute}".
SimpleForm will always look for a default attribute translation under the "defaults" key if no
specific is found inside the model key.Note that this syntax is different from 1.x. To migrate to
the new syntax, just move "labels.#{attribute}" to "labels.defaults.#{attribute}".
In addition, SimpleForm will fallback to default human_attribute_name from Rails when no other 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.
In addition, SimpleForm will fallback to default human_attribute_name from Rails when no other
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.
SimpleForm also has support for translating options in collection helpers. For instance, given a 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:
SimpleForm also has support for translating options in collection helpers. For instance, given a
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]
@ -545,7 +606,9 @@ en:
female: "Female'
```
You can also use the `defaults` key as you would do with labels, hints and placeholders. It is important to notice that SimpleForm will only do the lookup for options if you give a collection composed of symbols only. This is to avoid constant lookups to I18n.
You can also use the `defaults` key as you would do with labels, hints and placeholders. It is
important to notice that SimpleForm will only do the lookup for options if you give a collection
composed of symbols only. This is to avoid constant lookups to I18n.
It's also possible to translate buttons, using Rails' built-in I18n support:
@ -558,13 +621,21 @@ en:
update: "Save Changes"
```
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 "rails generate simple_form:install".
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
"rails generate simple_form:install".
## HTML 5 Notice
By default, SimpleForm will generate input field types and attributes that are supported in HTML5, 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.
By default, SimpleForm will generate input field types and attributes that are supported in HTML5,
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.
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.
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.
It is possible to disable all HTML 5 extensions in SimpleForm with the following configuration:
@ -572,13 +643,16 @@ It is possible to disable all HTML 5 extensions in SimpleForm with the following
SimpleForm.html5 = false # default is true
```
If you want to have all other HTML 5 features, such as the new field types, you can disable only the browser validation:
If you want to have all other HTML 5 features, such as the new field types, you can disable only
the browser validation:
```ruby
SimpleForm.browser_validations = false # default is true
```
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.
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.
You can also add `novalidate` to a specific form by setting the option on the form itself:
@ -586,11 +660,16 @@ You can also add `novalidate` to a specific form by setting the option on the fo
<%= 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. However, you can disable it if you want, by removing the placeholder component from the components list in SimpleForm configuration file.
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.
However, you can disable it if you want, by removing the placeholder component from the components
list in SimpleForm configuration file.
## Configuration
SimpleForm has several configuration values. You can read and change them in the initializer created by SimpleForm, so if you haven't executed the command below yet, please do:
SimpleForm has several configuration values. You can read and change them in the initializer created
by SimpleForm, so if you haven't executed the command below yet, please do:
`rails generate simple_form:install`