Forum: Ruby on Rails Validating a DateTime

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.
Mark P. (Guest)
on 2006-04-10 01:55
(Received via mailing list)
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
Conrad T. (Guest)
on 2006-04-10 02:26
(Received via mailing list)
Hi, please take a look at the parse method at the following site:

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

Peace,

-Conrad
Jonathan V. (Guest)
on 2006-04-10 02:50
(Received via mailing list)
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/valid...

-Jonathan
Mark P. (Guest)
on 2006-04-10 15:06
(Received via mailing list)
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 V. (Guest)
on 2006-04-10 16:52
(Received via mailing list)
On 4/10/06, Mark P. <removed_email_address@domain.invalid> 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.
Anonymous (Guest)
on 2006-04-27 04:33
(Received via mailing list)
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
This topic is locked and can not be replied to.