1
0
Fork 0
mirror of https://github.com/heartcombo/simple_form.git synced 2022-11-09 12:19:26 -05:00
heartcombo--simple_form/README.rdoc

360 lines
14 KiB
Text
Raw Normal View History

2009-12-10 12:57:24 -05:00
== SimpleForm
2009-11-18 12:45:35 -05:00
2009-12-11 15:01:15 -05:00
Forms made easy (for Rails)!
2009-11-18 12:45:35 -05:00
2010-06-29 15:53:48 -04:00
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.
2009-12-10 12:57:24 -05:00
2009-12-11 15:01:15 -05:00
== Installation
Install the gem:
2009-12-10 12:57:24 -05:00
2010-06-04 01:41:04 -04:00
gem install simple_form
2009-12-11 15:01:15 -05:00
2010-05-11 05:36:25 -04:00
Add it to your Gemfile:
gem "simple_form"
2009-12-11 15:01:15 -05:00
Run the generator:
2009-12-10 12:57:24 -05:00
rails generate simple_form:install
2010-02-06 13:29:41 -05:00
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
2010-06-04 01:41:04 -04:00
And you are ready to go. Since this branch aims Rails 3 support,
2010-02-06 13:29:41 -05:00
if you want to use it with Rails 2.3 you should check this branch:
2009-12-11 15:01:15 -05:00
2010-02-06 13:29:41 -05:00
http://github.com/plataformatec/simple_form/tree/v1.0
2009-12-11 15:01:15 -05:00
== Usage
2009-12-10 12:57:24 -05:00
2009-12-11 15:01:15 -05:00
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.
2009-12-11 13:23:29 -05:00
2009-12-10 12:57:24 -05:00
To start using SimpleForm you just have to use the helper it provides:
<%= simple_form_for @user do |f| %>
2010-02-07 10:58:23 -05:00
<%= f.input :username %>
<%= f.input :password %>
<%= f.button :submit %>
<% end %>
2009-12-10 12:57:24 -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).
2009-12-10 12:57:24 -05:00
2010-09-23 23:59:47 -04:00
You can overwrite the default label by passing it to the input method, add a hint or even a placeholder:
2009-12-10 12:57:24 -05:00
<%= simple_form_for @user do |f| %>
2010-02-07 10:58:23 -05:00
<%= f.input :username, :label => 'Your username please' %>
<%= f.input :password, :hint => 'No special characters.' %>
2010-09-23 23:59:47 -04:00
<%= f.input :email, :placeholder => 'user@domain.com' %>
2010-02-07 10:58:23 -05:00
<%= f.button :submit %>
<% end %>
2009-12-10 12:57:24 -05:00
2009-12-11 15:01:15 -05:00
You can also disable labels, hints or error or configure the html of any of them:
2009-12-10 12:57:24 -05:00
<%= simple_form_for @user do |f| %>
2010-02-07 10:58:23 -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 %>
<%= f.button :submit %>
<% end %>
2009-12-10 12:57:24 -05:00
It is also possible to pass any html attribute straight to the input, by using the :input_html option, for instance:
<%= 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.button :submit %>
<% end %>
2009-12-11 15:01:15 -05:00
By default all inputs are required, which means an * is prepended to the label, but you can disable it in any input you want:
2009-12-10 12:57:24 -05:00
<%= simple_form_for @user do |f| %>
2010-02-07 10:58:23 -05:00
<%= f.input :name, :required => false %>
<%= f.input :username %>
<%= f.input :password %>
<%= f.button :submit %>
<% end %>
2009-12-10 12:57:24 -05:00
2009-12-11 15:01:15 -05:00
SimpleForm also lets you overwrite the default input type it creates:
2009-12-10 12:57:24 -05:00
<%= simple_form_for @user do |f| %>
2010-02-07 10:58:23 -05:00
<%= f.input :username %>
<%= f.input :password %>
<%= f.input :description, :as => :text %>
<%= f.input :accepts, :as => :radio %>
<%= f.button :submit %>
<% end %>
2009-12-10 12:57:24 -05:00
2009-12-11 15:01:15 -05:00
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.
SimpleForm accepts same options as their corresponding input type helper in Rails:
<%= 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.button :submit %>
<% end %>
2009-12-11 15:01:15 -05:00
SimpleForm also allows you using label, hint and error helpers it provides:
2009-12-10 12:57:24 -05:00
<%= simple_form_for @user do |f| %>
2010-02-07 10:58:23 -05:00
<%= f.label :username %>
<%= f.text_field :username %>
<%= f.error :username, :id => 'user_name_error' %>
<%= f.hint 'No special characters, please!' %>
<%= f.submit 'Save' %>
<% end %>
2009-12-10 12:57:24 -05:00
2009-12-11 15:01:15 -05:00
Any extra option passed to label, hint or error will be rendered as html option.
=== Collections
2009-12-10 12:57:24 -05:00
2009-12-11 15:01:15 -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 overriding the :collection option:
2009-12-10 12:57:24 -05:00
<%= simple_form_for @user do |f| %>
2010-02-07 10:58:23 -05:00
<%= f.input :user %>
<%= f.input :age, :collection => 18..60 %>
<%= f.button :submit %>
<% end %>
2009-12-10 12:57:24 -05:00
2009-12-11 15:01:15 -05:00
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).
2009-12-10 12:57:24 -05:00
2009-12-11 15:01:15 -05:00
Collection inputs accepts two other options beside collections:
2009-12-11 15:01:15 -05:00
* label_method => the label method to be applied to the collection to retrieve the label
2009-12-11 15:01:15 -05:00
* 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 except lambda/procs incase 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:
2009-12-11 15:01:15 -05:00
f.input :age, :collection => 18..60, :prompt => "Select your age"
=== 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:
f.input :residence_country, :priority => [ "Brazil" ]
2009-12-11 15:01:15 -05:00
f.input :time_zone, :priority => /US/
2009-12-11 15:01:15 -05:00
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.
2009-12-11 13:23:29 -05:00
2009-12-11 15:01:15 -05:00
=== Wrapper
SimpleForm allows you to add a wrapper which contains the label, error, hint and input.
The first step is to configure a wrapper tag:
SimpleForm.wrapper_tag = :p
2009-12-11 15:01:15 -05:00
2010-06-04 01:41:04 -04:00
And now, you don't need to wrap your f.input calls anymore:
2009-12-11 15:01:15 -05:00
<%= simple_form_for @user do |f| %>
<%= f.input :username %>
<%= f.input :password %>
2010-02-07 10:58:23 -05:00
<%= f.button :submit %>
<% end %>
2009-12-11 15:01:15 -05:00
== Associations
2010-06-04 01:41:04 -04:00
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:
2009-12-11 13:23:29 -05:00
class User < ActiveRecord::Base
belongs_to :company
has_and_belongs_to_many :roles
end
class Company < ActiveRecord::Base
has_many :users
end
class Role < ActiveRecord::Base
has_and_belongs_to_many :users
end
Now we have the user form:
<%= simple_form_for @user do |f| %>
2010-02-07 10:58:23 -05:00
<%= f.input :name %>
<%= f.association :company %>
<%= f.association :roles %>
<%= f.button :submit %>
<% end %>
2009-12-11 13:23:29 -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 radios and check boxes as well:
2010-02-06 13:29:41 -05:00
f.association :company, :as => :radio
2010-06-04 01:41:04 -04:00
f.association :roles, :as => :check_boxes
2009-12-11 13:23:29 -05:00
2010-03-29 20:15:23 -04:00
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:
2009-12-11 13:23:29 -05:00
2009-12-11 15:01:15 -05:00
f.association :company, :collection => Company.active.all(:order => 'name'), :prompt => "Choose a Company"
2009-12-11 13:23:29 -05:00
2009-12-11 15:01:15 -05:00
== Buttons
2009-12-11 13:23:29 -05:00
2010-02-06 16:25:36 -05:00
All web forms need buttons, right? SimpleForm wraps them in the DSL, acting like a proxy:
2009-12-11 13:23:29 -05:00
<%= simple_form_for @user do |f| %>
2010-02-07 10:58:23 -05:00
<%= f.input :name %>
<%= f.button :submit %>
<% end %>
2009-12-11 13:23:29 -05:00
2010-02-06 16:25:36 -05:00
The above will simply call submit. You choose to use it or not, it's just a question of taste.
2009-12-11 13:23:29 -05:00
== Extra helpers
2009-12-11 15:01:15 -05:00
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.
2009-12-11 13:23:29 -05:00
=== Simple Fields For
2009-12-11 15:01:15 -05:00
Wrapper to use simple form inside a default rails form
2009-12-11 13:23:29 -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
end
end
=== Collection Radio
2009-12-11 15:01:15 -05:00
Creates a collection of radio inputs with labels associated (same API as collection_select):
2009-12-11 13:23:29 -05:00
form_for @user do |f|
f.collection_radio :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
end
<input id="user_options_true" name="user[options]" type="radio" value="true" />
<label class="collection_radio" for="user_options_true">Yes</label>
<input id="user_options_false" name="user[options]" type="radio" value="false" />
<label class="collection_radio" for="user_options_false">No</label>
=== Collection Check Box
2009-12-11 15:01:15 -05:00
Creates a collection of check boxes with labels associated (same API as collection_select):
2009-12-11 13:23:29 -05:00
form_for @user do |f|
f.collection_check_boxes :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
2009-12-11 13:23:29 -05:00
end
<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>
== Mappings/Inputs available
SimpleForm comes with a lot of default mappings:
Mapping Input Column Type
boolean check box boolean
string text field string
2010-03-29 20:15:23 -04:00
email email field string with name matching "email"
url url field string with name matching "url"
tel tel field string with name matching "phone"
2009-12-11 13:23:29 -05:00
password password field string with name matching "password"
search search -
2009-12-11 13:23:29 -05:00
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
2009-12-11 13:23:29 -05:00
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"
2009-12-10 12:57:24 -05:00
== I18n
2010-09-23 23:59:47 -04:00
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:
2009-12-10 12:57:24 -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.'
2010-09-23 23:59:47 -04:00
placeholders:
user:
username: 'Your username'
password: '****'
2009-12-10 12:57:24 -05:00
2010-09-23 23:59:47 -04:00
And your forms will use this information to render the components for you.
2009-12-10 12:57:24 -05:00
2010-09-23 23:59:47 -04:00
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:
2009-12-10 12:57:24 -05:00
en:
simple_form:
labels:
user:
username: 'User name'
password: 'Password'
2009-12-10 12:57:24 -05:00
edit:
username: 'Change user name'
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 skip the model information inside it:
en:
simple_form:
labels:
username: 'User name'
password: 'Password'
hints:
username: 'User name to sign in.'
password: 'No special characters, please.'
2010-09-23 23:59:47 -04:00
placeholders:
username: 'Your username'
password: '****'
2009-12-10 12:57:24 -05:00
2010-09-23 23:59:47 -04:00
SimpleForm will always look for a default attribute translation if no specific is found inside the model key. In addition, SimpleForm will fallback to default human_attribute_name from Rails when no other translation is found for labels.
2009-12-10 12:57:24 -05:00
2010-09-23 23:59:47 -04:00
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.
2009-12-10 12:57:24 -05:00
2010-09-29 07:37:06 -04:00
It's also possible to translate buttons, using Rails' built-in I18n support:
en:
helpers:
2010-09-29 07:37:06 -04:00
submit:
user:
create: "Add %{model}"
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".
2010-02-06 13:25:46 -05:00
== Configuration
2009-12-11 13:23:29 -05:00
2009-12-11 15:01:15 -05:00
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:
2009-12-11 13:23:29 -05:00
rails generate simple_form:install
2009-12-11 13:23:29 -05:00
2009-11-18 12:45:35 -05:00
== TODO
Please refer to TODO file.
2010-02-06 13:25:46 -05:00
== Maintainers
2009-12-11 13:23:29 -05:00
* José Valim (http://github.com/josevalim)
* Carlos Antonio da Silva (http://github.com/carlosantoniodasilva)
== Bugs and Feedback
If you discover any bugs or want to drop a line, feel free to create an issue on GitHub.
http://github.com/plataformatec/simple_form/issues
2009-11-18 12:45:35 -05:00
2010-02-06 13:25:46 -05:00
MIT License. Copyright 2010 Plataforma Tecnologia. http://blog.plataformatec.com.br