Forum: Ruby on Rails Date Validation

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Sainaba S. (Guest)
on 2006-01-05 14:20
Hi,

How can I validate date in model class

Thanks.

Sainaba.
Sean S. (Guest)
on 2006-01-10 11:51
> 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?
Alex Y. (Guest)
on 2006-01-10 12:12
(Received via mailing list)
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
Jan TopiÅ?ski (Guest)
on 2006-01-25 16:10
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-.... How
ever I view Ara T Howard solution as a "fix to the immediate problem"
rather then final solution.

hope it helps.

jan
This topic is locked and can not be replied to.