Customize input of type time simple_form

0

I need you to have this range: 8:00 AM - 8:00 PM

= f.input :start , :ampm => true, :minute_step => 60, :prompt => { :Hora => "Hour", :minutos => false }, :required => false
    
asked by Carlos Uriel Patiño Santiago 17.09.2016 в 03:12
source

1 answer

1

By default simple_form does not have the option that you require, however you could create a custom input in which you determine the range of hours:

# app/inputs/hour_input.rb
class HourInput < SimpleForm::Inputs::Base
  def input(wrapper_options = nil)
    merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)

    input_options[:selected] = selected_value
    @builder.select(attribute_name, hour_options, input_options, merged_input_options)
  end

  private

  def hour_options
    hour = [['Selecione hora', '00:00:00']]
    (8..20).each do |h|
      hour << ["#{h}:00", format('%02d:00:00', h)]
    end
    hour
  end

  def selected_value
    value = object.send(attribute_name)
    value && value.strftime('%H:%M:%S')
  end
end

And then call from the view with = f.input :start , as: :hour

    
answered by 20.09.2016 в 21:04