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 On 4/9/06, Mark Paxton <mark@paxton.plus.com> wrote:> 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 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
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, Conrad Taylor <conradwt@gmail.com> wrote:> > Hi, please take a look at the parse method at the following site: > > http://www.ruby-doc.org/core/classes/DateTime.html > > Peace, > > -Conrad > > On 4/9/06, Mark Paxton <mark@paxton.plus.com> wrote: > > 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 > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060409/e2c663a9/attachment.html
Jonathan Viney 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 Taylor wrote:> Hi, please take a look at the parse method at the following site: > > http://www.ruby-doc.org/core/classes/DateTime.htmlThanks 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.
On 4/10/06, Mark Paxton <mark@paxton.plus.com> 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. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060410/dd336b1a/attachment.html
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
Not currently, it will very soon though. I might get time today or tomorrow to implement it. Feel free to send me a patch if you feel so inclined! -Jonathan. On 4/27/06, Anonymous <list@freeonrails.com> wrote:> > > 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 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060427/4953e9bf/attachment.html
Maybe Matching Threads
- validates_date plugin for ActiveRecord
- Date and Time validator, updater
- date_helper megapatch and MultiparameterAssignmentErrors
- Help with overriding / overwriting date values in ActiveRecord to accept Euro Style Date from a Textbox
- validates_dateness_of ?