Method: DateValidator#validate_each

Defined in:
lib/validators/date_validator.rb

#validate_each(record, attribute, value) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/validators/date_validator.rb', line 7

def validate_each(record, attribute, value)

  @error_hash = options[:error_level].present? ? record.send(options[:error_level]) : record.errors

  original_value = value
  original_value = record.read_attribute_before_type_cast( attribute ) rescue nil # only in rails
  # dont display date format error unless date could not be parsed
  if value.nil?
    # if blank date was given it's still not a format issue, still show message if desired
    return if original_value.blank? && options[:allow_blank]

    # display helpful date format validation message with original value
    message = options[:time] ?  ": #{original_value} is not a valid value. Value must be a date in YYYY-MM-DD or YYYY-MM-DD HH:MM:SS format." : ": #{original_value} is not a valid value. Value must be a date in YYYY-MM-DD format."
    @error_hash.add(attribute, (options[:message] || message))
  else
    # handle validation options on valid date instances
    return unless value.respond_to?(:strftime)
    if(options[:after] && (after_message = validate_after_option(record, attribute, value, original_value)) != true)
      @error_hash.add(attribute, (options[:after_message] || "#{after_message}."))
    end
    if(options[:before] && (before_message = validate_before_option(record, attribute, value, original_value)) != true)
      @error_hash.add(attribute, (options[:before_message] || "#{before_message}."))
    end
  end
end