Validating a DateTime

Hi,

Does anyone know of a method to validate a DateTime field?

I’ve looked at the validation docs and the validates_date /
validates_time plug-in but I can’t find a method to validate a value in
the format of yyyymmddhhmm. It seems like something that should exist
already, so I’d just like to check before I attempt my own version…

Cheers,
Mark

Hi, please take a look at the parse method at the following site:

http://www.ruby-doc.org/core/classes/DateTime.html

Peace,

-Conrad

It should be reasonably straight forward to add a validates_date_time
method
to my plugin. Just adapt the validator and regex code for the kind of
input
you want.

http://svn.viney.net.nz/things/rails/plugins/validates_date_time

-Jonathan

On 4/10/06, Mark P. [email protected] wrote:

Is there a particular reason that validates_date and validates_time
doesn’t use the above method for Date and Time?

ie. Date.parse(value_before_type_cast.to_s) Rescue { errors.add… }

Yes. It guesses wrongly too often when the date is a bit ambiguous. And
also
doesn’t support some of the input formats I wanted.

I’ve messed around with the plugin and got it doing something

approaching what I need, but it’s not very tidy yet.

Email it to me off list if you want. I’ll have a look. I don’t mind
spending
a bit of time adding this sort of functionality to the plugin, I’m sure
others will find it useful and I will probably need it in a month or so
anyway.

My other problem is now that I have date and time on the form in

seperate text boxes which are combined to be saved as one DateTime
field. Is there a way to validate the input from a form that isn’t a
field in the model?

I’m sure we can figure something out. I think if you define {attr} and
{attr}= methods you can use the validators as normal. Untried though.

-Jonathan.

Jonathan V. wrote:

It should be reasonably straight forward to add a validates_date_time
method to my plugin. Just adapt the validator and regex code for the
kind of input you want.

Conrad T. wrote:

Hi, please take a look at the parse method at the following site:

http://www.ruby-doc.org/core/classes/DateTime.html

Thanks for the help…

Is there a particular reason that validates_date and validates_time
doesn’t use the above method for Date and Time?

ie. Date.parse(value_before_type_cast.to_s) Rescue { errors.add… }

I’ve messed around with the plugin and got it doing something
approaching what I need, but it’s not very tidy yet.

My other problem is now that I have date and time on the form in
seperate text boxes which are combined to be saved as one DateTime
field. Is there a way to validate the input from a form that isn’t a
field in the model?

Cheers,
Mark

This message has been checked for viruses but the contents of an
attachment
may still contain software viruses, which could damage your computer
system:
you are advised to perform your own checks. Email communications with
the
University of Nottingham may be monitored as permitted by UK
legislation.

Jonathan, does the validates_time support the :before and :after format?

ie. validates_time :office_time, :after => “9am”, :before=>“5pm”

I tried it in the code, and there is no compilation error, but it failed
my unit tests

  • thanks