Method: SimpleFormWithClientValidation::FormBuilder#input
- Defined in:
- lib/simple_form_with_client_validation/form_builder.rb
#input(attribute_name, options = {}, &block) ⇒ Object Also known as: attribute
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.
Examples
# 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]" size="100" 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 SimpleFormWithClientValidation 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.
Options
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
Collection
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
Priority
Some inputs, as :time_zone and :country accepts a :priority option. If none is given SimpleFormWithClientValidation.time_zone_priority and
SimpleFormWithClientValidation.country_priority are used respectivelly.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/simple_form_with_client_validation/form_builder.rb', line 106 def input(attribute_name, ={}, &block) = @defaults.deep_dup.deep_merge() if @defaults #SimpleFormWithClientValidation.wrapper(name) is a module method that # Retrieves a given wrapper chosen = if name = [:wrapper] name.respond_to?(:render) ? name : SimpleFormWithClientValidation.wrapper(name) else wrapper end chosen.render find_input(attribute_name, , &block) end |