validate date and time inputs
This commit is contained in:
parent
8c0df3b4e8
commit
c64a7ba3cd
3 changed files with 82 additions and 9 deletions
|
@ -1,7 +1,9 @@
|
||||||
# DateTime picker using bootstrap-datepicker for the time part
|
# 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
|
# Requires +date_time_attribute+ gem (+workaround) and active on the attribute.
|
||||||
# https://github.com/einzige/date_time_attribute
|
# @see DateTimeAttributeValidate
|
||||||
|
# @see http://stackoverflow.com/a/20317763/2866660
|
||||||
|
# @see https://github.com/einzige/date_time_attribute
|
||||||
class DatePickerTimeInput < SimpleForm::Inputs::StringInput
|
class DatePickerTimeInput < SimpleForm::Inputs::StringInput
|
||||||
def input
|
def input
|
||||||
# Date format must match datepicker's, see app/assets/application.js .
|
# 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
|
# 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).
|
# double widgets, and perhaps conditional css to adjust input width (chrome).
|
||||||
value = @builder.object.send attribute_name
|
value = @builder.object.send attribute_name
|
||||||
date_options = {as: :string, class: 'input-small datepicker', value: value.try {|e| e.strftime('%Y-%m-%d')}}
|
date_options = {as: :string, class: 'input-small datepicker'}
|
||||||
time_options = {as: :string, class: 'input-mini', value: value.try {|e| e.strftime('%H:%M')}}
|
time_options = {as: :string, class: 'input-mini'}
|
||||||
@builder.input_field("#{attribute_name}_date", input_html_options.merge(date_options)) + ' ' +
|
@builder.input_field("#{attribute_name}_date_value", input_html_options.merge(date_options)) + ' ' +
|
||||||
@builder.input_field("#{attribute_name}_time", input_html_options.merge(time_options))
|
@builder.input_field("#{attribute_name}_time_value", input_html_options.merge(time_options))
|
||||||
# time_select requires a date_select
|
# time_select requires a date_select
|
||||||
#@builder.time_select("#{attribute_name}_time", {ignore_date: true}, input_html_options.merge(time_options))
|
#@builder.time_select("#{attribute_name}_time", {ignore_date: true}, input_html_options.merge(time_options))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def label_target
|
||||||
|
"#{attribute_name}_date_value"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -32,7 +32,8 @@ class Order < ActiveRecord::Base
|
||||||
scope :recent, -> { order('starts DESC').limit(10) }
|
scope :recent, -> { order('starts DESC').limit(10) }
|
||||||
|
|
||||||
# Allow separate inputs for date and time
|
# Allow separate inputs for date and time
|
||||||
include DateTimeAttribute
|
# with workaround for https://github.com/einzige/date_time_attribute/issues/14
|
||||||
|
include DateTimeAttributeValidate
|
||||||
date_time_attribute :starts, :ends
|
date_time_attribute :starts, :ends
|
||||||
|
|
||||||
def stockit?
|
def stockit?
|
||||||
|
|
66
lib/date_time_attribute_validate.rb
Normal file
66
lib/date_time_attribute_validate.rb
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
# workaround for https://github.com/einzige/date_time_attribute/issues/14
|
||||||
|
require 'date_time_attribute'
|
||||||
|
|
||||||
|
module DateTimeAttributeValidate
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
include DateTimeAttribute
|
||||||
|
|
||||||
|
module ClassMethods
|
||||||
|
|
||||||
|
def date_time_attribute(*attributes)
|
||||||
|
super
|
||||||
|
|
||||||
|
attributes.each do |attribute|
|
||||||
|
|
||||||
|
validate "#{attribute}_datetime_value_valid"
|
||||||
|
|
||||||
|
# allow resetting the field to nil
|
||||||
|
before_validation do
|
||||||
|
if self.instance_variable_get("@#{attribute}_is_set")
|
||||||
|
date = self.instance_variable_get("@#{attribute}_date_value")
|
||||||
|
time = self.instance_variable_get("@#{attribute}_time_value")
|
||||||
|
if date.blank? and time.blank?
|
||||||
|
self.send("#{attribute}=", nil)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# remember old date and time values
|
||||||
|
define_method("#{attribute}_date_value=") do |val|
|
||||||
|
self.instance_variable_set("@#{attribute}_is_set", true)
|
||||||
|
self.instance_variable_set("@#{attribute}_date_value", val)
|
||||||
|
self.send("#{attribute}_date=", val) rescue nil
|
||||||
|
end
|
||||||
|
define_method("#{attribute}_time_value=") do |val|
|
||||||
|
self.instance_variable_set("@#{attribute}_is_set", true)
|
||||||
|
self.instance_variable_set("@#{attribute}_time_value", val)
|
||||||
|
self.send("#{attribute}_time=", val) rescue nil
|
||||||
|
end
|
||||||
|
|
||||||
|
# fallback to field when values are not set
|
||||||
|
define_method("#{attribute}_date_value") do
|
||||||
|
self.instance_variable_get("@#{attribute}_date_value") || self.send("#{attribute}_date").try {|e| e.strftime('%Y-%m-%d')}
|
||||||
|
end
|
||||||
|
define_method("#{attribute}_time_value") do
|
||||||
|
self.instance_variable_get("@#{attribute}_time_value") || self.send("#{attribute}_time").try {|e| e.strftime('%H:%m')}
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
# validate date and time
|
||||||
|
define_method("#{attribute}_datetime_value_valid") do
|
||||||
|
date = self.instance_variable_get("@#{attribute}_date_value")
|
||||||
|
unless date.blank? or (Date.parse(date) rescue nil)
|
||||||
|
errors.add(attribute, "is not a valid date") # @todo I18n
|
||||||
|
end
|
||||||
|
time = self.instance_variable_get("@#{attribute}_time_value")
|
||||||
|
unless time.blank? or (Time.parse(time) rescue nil)
|
||||||
|
errors.add(attribute, "is not a valid time") # @todo I18n
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue