Class: ActiveModel::Validations::CompleteDateValidator

Inherits:
EachValidator
  • Object
show all
Defined in:
lib/common_lib/active_model/validations/complete_date.rb

Instance Method Summary collapse

Instance Method Details

#validate(record) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/common_lib/active_model/validations/complete_date.rb', line 15

def validate(record)
	[attributes].flatten.each do |attribute|
		value = record.send("#{attribute}_before_type_cast")
		if( value.is_a?(String) and !value.blank? )
			date_hash = Date._parse(value)
			#	>> Date._parse( '1/10/2011')
			#	=> {:mon=>1, :year=>2011, :mday=>10}
			unless date_hash.has_key?(:year) &&
					date_hash.has_key?(:mon) &&
					date_hash.has_key?(:mday)
				#	associated default error message is in config/locales/en.yml
				record.errors.add(attribute, :incomplete_date, options)
			end
		end
	end
end