Search code examples
ruby-on-railsruby-on-rails-4simple-formsimple-form-for

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'?


Solution

  • 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