validate date and time inputs

This commit is contained in:
wvengen 2014-06-02 15:47:29 +02:00
parent 8c0df3b4e8
commit c64a7ba3cd
3 changed files with 82 additions and 9 deletions

View file

@ -1,7 +1,9 @@
# DateTime picker using bootstrap-datepicker for the time part
# requires `date_time_attribute` gem and active on the attribute
# http://stackoverflow.com/a/20317763/2866660
# https://github.com/einzige/date_time_attribute
# DateTime picker using bootstrap-datepicker for the time part.
#
# Requires +date_time_attribute+ gem (+workaround) and active on the attribute.
# @see DateTimeAttributeValidate
# @see http://stackoverflow.com/a/20317763/2866660
# @see https://github.com/einzige/date_time_attribute
class DatePickerTimeInput < SimpleForm::Inputs::StringInput
def input
# Date format must match datepicker's, see app/assets/application.js .
@ -9,11 +11,15 @@ class DatePickerTimeInput < SimpleForm::Inputs::StringInput
# In the future, use html5 date&time inputs. This needs modernizr or equiv. to avoid
# double widgets, and perhaps conditional css to adjust input width (chrome).
value = @builder.object.send attribute_name
date_options = {as: :string, class: 'input-small datepicker', value: value.try {|e| e.strftime('%Y-%m-%d')}}
time_options = {as: :string, class: 'input-mini', value: value.try {|e| e.strftime('%H:%M')}}
@builder.input_field("#{attribute_name}_date", input_html_options.merge(date_options)) + ' ' +
@builder.input_field("#{attribute_name}_time", input_html_options.merge(time_options))
date_options = {as: :string, class: 'input-small datepicker'}
time_options = {as: :string, class: 'input-mini'}
@builder.input_field("#{attribute_name}_date_value", input_html_options.merge(date_options)) + ' ' +
@builder.input_field("#{attribute_name}_time_value", input_html_options.merge(time_options))
# time_select requires a date_select
#@builder.time_select("#{attribute_name}_time", {ignore_date: true}, input_html_options.merge(time_options))
end
def label_target
"#{attribute_name}_date_value"
end
end