| Class | SimpleForm::FormBuilder |
| In: |
lib/simple_form/form_builder.rb
|
| Parent: | ActionView::Helpers::FormBuilder |
| ACTIONS | = | { 'create' => 'new', 'update' => 'edit' | When action is create or update, we still should use new and edit | |
| ATTRIBUTE_COMPONENTS | = | %i[html5 min_max maxlength minlength placeholder pattern readonly] |
| object | [R] | |
| object_name | [R] | |
| template | [R] | |
| wrapper | [R] |
Helper for dealing with association selects/radios, generating the collection automatically. It‘s just a wrapper to input, so all options supported in input are also supported by association. Some extra options can also be given:
simple_form_for @user do |f|
f.association :company # Company.all
end
f.association :company, collection: Company.all(order: 'name')
# Same as using :order option, but overriding collection
When a block is given, association simple behaves as a proxy to simple_fields_for:
f.association :company do |c|
c.input :name
c.input :type
end
From the options above, only :collection can also be supplied.
Please note that the association helper is currently only tested with Active Record. Depending on the ORM you are using your mileage may vary.
Creates a collection of check boxes for each item in the collection, associated with a clickable label. Use value_method and text_method to convert items in the collection for use as text/value in check boxes. You can give a symbol or a proc to both value_method and text_method, that will be evaluated for each item in the collection.
form_for @user do |f|
f.collection_check_boxes :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
end
<input name="user[options][]" type="hidden" value="" />
<input id="user_options_true" name="user[options][]" type="checkbox" value="true" />
<label class="collection_check_boxes" 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_boxes" for="user_options_false">No</label>
It is also possible to give a block that should generate the check box + label. To wrap the check box with the label, for instance:
form_for @user do |f|
f.collection_check_boxes(
:options, [[true, 'Yes'] ,[false, 'No']], :first, :last
) do |b|
b.label { b.check_box + b.text }
end
end
Collection check box accepts some extra options:
* checked => the value or values that should be checked initially. Accepts
a single item or an array of items. It overrides existing associations.
* disabled => the value or values that should be disabled. Accepts a single
item or an array of items.
* collection_wrapper_tag => the tag to wrap the entire collection.
* collection_wrapper_class => the CSS class to use for collection_wrapper_tag. This option
is ignored if the :collection_wrapper_tag option is blank.
* item_wrapper_tag => the tag to wrap each item in the collection.
* item_wrapper_class => the CSS class to use for item_wrapper_tag
* a block => to generate the label + check box or any other component.
Create a collection of radio inputs for the attribute. Basically this helper will create a radio input associated with a label for each text/value option in the collection, using value_method and text_method to convert these text/value. You can give a symbol or a proc to both value_method and text_method, that will be evaluated for each item in the collection.
form_for @user do |f|
f.collection_radio_buttons :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
end
<input id="user_options_true" name="user[options]" type="radio" value="true" />
<label class="collection_radio_buttons" for="user_options_true">Yes</label>
<input id="user_options_false" name="user[options]" type="radio" value="false" />
<label class="collection_radio_buttons" for="user_options_false">No</label>
It is also possible to give a block that should generate the radio + label. To wrap the radio with the label, for instance:
form_for @user do |f|
f.collection_radio_buttons(
:options, [[true, 'Yes'] ,[false, 'No']], :first, :last
) do |b|
b.label { b.radio_button + b.text }
end
end
Collection radio accepts some extra options:
* checked => the value that should be checked initially.
* disabled => the value or values that should be disabled. Accepts a single
item or an array of items.
* collection_wrapper_tag => the tag to wrap the entire collection.
* collection_wrapper_class => the CSS class to use for collection_wrapper_tag
* item_wrapper_tag => the tag to wrap each item in the collection.
* item_wrapper_class => the CSS class to use for item_wrapper_tag
* a block => to generate the label + radio or any other component.
Creates an error notification message that only appears when the form object has some error. You can give a specific message with the :message option, otherwise it will look for a message using I18n. All other options given are passed straight as html options to the html tag.
f.error_notification f.error_notification message: 'Something went wrong' f.error_notification id: 'user_error_message', class: 'form_error'
Return the error but also considering its name. This is used when errors for a hidden field need to be shown.
f.full_error :token #=> <span class="error">Token is invalid</span>
Basic input helper, combines all components in the stack to generate input html based on options the user define and some guesses through database column information. By default a call to input will generate label + input + hint (when defined) + errors (when exists), and all can be configured inside a wrapper html.
If a block is given, the contents of the block will replace the input field that would otherwise be generated automatically. The content will be given a label and wrapper div to make it consistent with the other elements in the form.
# Imagine @user has error "can't be blank" on name
simple_form_for @user do |f|
f.input :name, hint: 'My hint'
end
This is the output html (only the input portion, not the form):
<label class="string required" for="user_name">
<abbr title="required">*</abbr> Super User Name!
</label>
<input class="string required" id="user_name" maxlength="100"
name="user[name]" type="text" value="Carlos" />
<span class="hint">My hint</span>
<span class="error">can't be blank</span>
Each database type will render a default input, based on some mappings and heuristic to determine which is the best option.
You have some options for the input to enable/disable some functions:
as: allows you to define the input type you want, for instance you
can use it to generate a text field for a date column.
required: defines whether this attribute is required or not. True
by default.
The fact SimpleForm is built in components allow the interface to be unified. So, for instance, if you need to disable :hint for a given input, you can pass hint: false. The same works for :error, :label and :wrapper.
Besides the html for any component can be changed. So, if you want to change the label html you just need to give a hash to :label_html. To configure the input html, supply :input_html instead and so on.
Some inputs, as datetime, time and select allow you to give extra options, like prompt and/or include blank. Such options are given in plainly:
f.input :created_at, include_blank: true
When playing with collections (:radio_buttons, :check_boxes and :select inputs), you have three extra options:
collection: use to determine the collection to generate the radio or select label_method: the method to apply on the array collection to get the label value_method: the method to apply on the array collection to get the value
Some inputs, as :time_zone and :country accepts a :priority option. If none is given SimpleForm.time_zone_priority and SimpleForm.country_priority are used respectively.
Creates a input tag for the given attribute. All the given options are sent as :input_html.
simple_form_for @user do |f|
f.input_field :name
end
This is the output html (only the input portion, not the form):
<input class="string required" id="user_name" maxlength="100"
name="user[name]" type="text" value="Carlos" />
It also support validation classes once it is configured.
# config/initializers/simple_form.rb
SimpleForm.setup do |config|
config.input_field_valid_class = 'is-valid'
config.input_field_error_class = 'is-invalid'
end
simple_form_for @user do |f|
f.input_field :name
end
When the validation happens, the input will be rendered with the class configured according to the validation:
Creates a default label tag for the given attribute. You can give a label through the :label option or using i18n. All the given options are sent as :label_html.
f.label :name # Do I18n lookup f.label :name, "Name" # Same behavior as Rails, do not add required tag f.label :name, label: "Name" # Same as above, but adds required tag f.label :name, required: false f.label :name, id: "cool_label"