Method: SimpleForm::FormBuilder#input_field

Defined in:
lib/simple_form/form_builder.rb

#input_field(attribute_name, options = {}) ⇒ Object

Creates a input tag for the given attribute. All the given options are sent as :input_html.

Examples

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:

  • when the input is valid:

    <input class="is-valid string required" id="user_name" value="Carlos" />
    
  • when the input is invalid:

    <input class="is-invalid string required" id="user_name" value="" />
    


165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/simple_form/form_builder.rb', line 165

def input_field(attribute_name, options = {})
  components = (wrapper.components.map(&:namespace) & ATTRIBUTE_COMPONENTS)

  options = options.dup
  options[:input_html] = options.except(:as, :boolean_style, :collection, :disabled, :label_method, :value_method, :prompt, *components)
  options = @defaults.deep_dup.deep_merge(options) if @defaults

  input      = find_input(attribute_name, options)
  wrapper    = find_wrapper(input.input_type, options)
  components = build_input_field_components(components.push(:input))

  SimpleForm::Wrappers::Root.new(components, wrapper.options.merge(wrapper: false)).render input
end