mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-25 07:16:41 +00:00
add :format option to validate and parse with specific a format
This commit is contained in:
@@ -161,20 +161,26 @@ module ValidatesTimeliness
|
||||
# Loop through format expressions for type and call proc on matches. Allow
|
||||
# pre or post match strings to exist if strict is false. Otherwise wrap
|
||||
# regexp in start and end anchors.
|
||||
# Returns 7 part time array.
|
||||
# Returns time array if matches a format, nil otherwise.
|
||||
def parse(string, type, options={})
|
||||
return string unless string.is_a?(String)
|
||||
options.reverse_merge!(:strict => true)
|
||||
|
||||
sets = if options[:format]
|
||||
[ send("#{type}_expressions").assoc(options[:format]) ]
|
||||
else
|
||||
expression_set(type, string)
|
||||
end
|
||||
|
||||
matches = nil
|
||||
exp, processor = expression_set(type, string).find do |regexp, proc|
|
||||
processor = sets.each do |format, regexp, proc|
|
||||
full = /\A#{regexp}\Z/ if options[:strict]
|
||||
full ||= case type
|
||||
when :date then /\A#{regexp}/
|
||||
when :time then /#{regexp}\Z/
|
||||
when :datetime then /\A#{regexp}\Z/
|
||||
end
|
||||
matches = full.match(string.strip)
|
||||
break(proc) if matches = full.match(string.strip)
|
||||
end
|
||||
last = options[:include_offset] ? 8 : 7
|
||||
processor.call(*matches[1..last]) if matches
|
||||
@@ -258,7 +264,7 @@ module ValidatesTimeliness
|
||||
end
|
||||
|
||||
def compile_formats(formats)
|
||||
formats.map { |format| regexp, format_proc = format_expression_generator(format) }
|
||||
formats.map { |format| [ format, *format_expression_generator(format) ] }
|
||||
end
|
||||
|
||||
# Pick expression set and combine date and datetimes for
|
||||
|
||||
@@ -14,8 +14,8 @@ module ValidatesTimeliness
|
||||
}
|
||||
|
||||
VALID_OPTIONS = [
|
||||
:on, :if, :unless, :allow_nil, :empty, :allow_blank, :blank,
|
||||
:with_time, :with_date, :ignore_usec,
|
||||
:on, :if, :unless, :allow_nil, :empty, :allow_blank,
|
||||
:with_time, :with_date, :ignore_usec, :format,
|
||||
:invalid_time_message, :invalid_date_message, :invalid_datetime_message
|
||||
] + RESTRICTION_METHODS.keys.map {|option| [option, "#{option}_message".to_sym] }.flatten
|
||||
|
||||
@@ -29,14 +29,17 @@ module ValidatesTimeliness
|
||||
end
|
||||
|
||||
def call(record, attr_name, value)
|
||||
value = ValidatesTimeliness::Parser.parse(value, type, :strict => false) if value.is_a?(String)
|
||||
raw_value = raw_value(record, attr_name) || value
|
||||
|
||||
if value.is_a?(String) || @configuration[:format]
|
||||
strict = !@configuration[:format].nil?
|
||||
value = ValidatesTimeliness::Parser.parse(raw_value, type, :strict => strict, :format => @configuration[:format])
|
||||
end
|
||||
|
||||
return if (raw_value.nil? && configuration[:allow_nil]) || (raw_value.blank? && configuration[:allow_blank])
|
||||
|
||||
add_error(record, attr_name, :blank) and return if raw_value.blank?
|
||||
|
||||
add_error(record, attr_name, "invalid_#{type}".to_sym) and return unless value
|
||||
add_error(record, attr_name, "invalid_#{type}".to_sym) and return if value.nil?
|
||||
|
||||
validate_restrictions(record, attr_name, value)
|
||||
end
|
||||
@@ -107,7 +110,6 @@ module ValidatesTimeliness
|
||||
|
||||
def add_error(record, attr_name, message, interpolate=nil)
|
||||
if defined?(I18n)
|
||||
# use i18n support in AR for message or use custom message passed to validation method
|
||||
custom = custom_error_messages[message]
|
||||
record.errors.add(attr_name, custom || message, interpolate || {})
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user