Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simple_form - override default input "type" mapping

simple_form is generating "type='number'" on the input field for any integer attribute, instead of type='text'. As this causes Chrome to show a counter control, I'd rather make it just use type='text' as the default for numbers.

It seems to be possible to override defaults in config/intializers/simple_form.rb, but it's not clear from the docs how to do this exactly. What's the syntax to set numberic columns/attributes to render as type='text'?

like image 530
mahemoff Avatar asked Dec 06 '14 11:12

mahemoff


1 Answers

You can override the default mapping on a per-field basis by specifying an input type:

<%= f.input :age, as: :string %>

(The full list of mappings is here.)

But if you want to eradicate numeric inputs from your project, try:

# config/initializers/simple_form.rb (before/after the SimpleForm.setup block, if this exists) 
module SimpleForm
  class FormBuilder < ActionView::Helpers::FormBuilder
    map_type :integer, :decimal, :float, to: SimpleForm::Inputs::StringInput
  end
end
like image 196
TK-421 Avatar answered Oct 16 '22 16:10

TK-421