Topic: is there a way to validate a date as being real?

hello there all,

I am looking for a way  to validate a date as being a real date.
if someone tries to enter something like February 31, it crashes my app.
is there a simple way to do this? i could not find anything in the validations stuff for it.

thanks

Re: is there a way to validate a date as being real?

You can just rescue to return nil upon parsing the date:

date = Date.parse('Feb 31, 2006') rescue nil

Or you can get a little more fancy:

begin
  date = Date.parse('Feb 31, 2006')
rescue ArgumentError
  # do something to report the error
end

Railscasts - Free Ruby on Rails Screencasts

Re: is there a way to validate a date as being real?

ok, so if a form date_select is passed to an action that was created by a scaffold, i can interrupt the action that saves the record ?

this is a bit easier than i thought.

thanks much

Re: is there a way to validate a date as being real?

Hmm, I'm not really sure the best way to handle invalid dates submitted through date_select. I would have to do some testing before I can tell if this is the best way to handle that.

Railscasts - Free Ruby on Rails Screencasts

Re: is there a way to validate a date as being real?

ok, well if you have a suggestion, please let me know,
and thanks for your help