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, = {}) components = (wrapper.components.map(&:namespace) & ATTRIBUTE_COMPONENTS) = .dup [:input_html] = .except(:as, :boolean_style, :collection, :disabled, :label_method, :value_method, :prompt, *components) = @defaults.deep_dup.deep_merge() if @defaults input = find_input(attribute_name, ) wrapper = find_wrapper(input.input_type, ) components = build_input_field_components(components.push(:input)) SimpleForm::Wrappers::Root.new(components, wrapper..merge(wrapper: false)).render input end |