Simple_form - override default enter display

simple_form generates "type = 'number" in the input field for any integer attribute instead of type =' text '. Since this causes Chrome to display a counter control, I would rather just use type = 'text' as the default value for numbers.

It seems possible to override the defaults in config / intializers / simple_form.rb, but it is not clear from the docs how to do this. What syntax sets numeric columns / attributes to display as type = 'text'?

+6
source share
2 answers

You can override the default mapping for each field by specifying the input type:

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

(A complete list of mappings is here .)

But if you want to root out numerical 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 
+10
source

For the record:

Use HTML5 Date, Time, Date, and Time Data

 SimpleForm::Inputs::DateTimeInput.class_eval do def use_html5_inputs?; input_options[:html5] || true end end 

Use string to enter date, time, date and time

Useful if you intend to use the datetime collector, for example bootstrap-datetimepicker :

 SimpleForm::FormBuilder.class_eval do def input(attribute_name, options = {}, &block) options = @defaults.deep_dup.deep_merge(options) if @defaults input = find_input(attribute_name, options, &block) # Add native DB type as CSS class so inputs can be filtered by that input.input_html_classes << input.column&.type # Use another attribute: # input.input_html_options[:'data-db-type']= input.column&.type wrapper = find_wrapper(input.input_type, options) wrapper.render input end # map_type :date, :time, :datetime, to: SimpleForm::Inputs::StringInput alias old_default_input_type default_input_type def default_input_type(attribute_name, column, options) if column.type.in? %i(date time datetime) :string else old_default_input_type(attribute_name, column, options) end end end 

map_type not required.

0
source

Source: https://habr.com/ru/post/979219/


All Articles