Date Validation

Hi,

How can I validate date in model class

Thanks.

Sainaba.

How can I validate date in model class

Thanks.

Sainaba.

Hi Sainaba,

Did you ever find an answer to this? I’m also searching for a way to
(for example) validate bad dates such as February 31st, 2006. Anyone
got a tip?

Sean S. wrote:

(for example) validate bad dates such as February 31st, 2006. Anyone
got a tip?

Date.valid_date? 2006,2,14
=> 2453781 # Julian day number

Date.valid_date? 2006,2,31
=> nil

This code will work in the model unless you are using date_select on the
form.

validates_each( :date ) do |record, name|
value = record.send(“#{name}_before_type_cast”.to_sym)
unless value.nil? || value.empty?
Date.parse(value) rescue record.errors.add(name, “date is not valid”)
end
end

In the letter case nothing will work except of this solution
http://wrath.rubyonrails.org/pipermail/rails/2005-March/004619.html. How
ever I view Ara T Howard solution as a “fix to the immediate problem”
rather then final solution.

hope it helps.

jan