Forum: Ruby on Rails [ANN] validates_date plugin for ActiveRecord

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.
B07fcd81d491ce2209d7995c4a851f9a?d=identicon&s=25 Jonathan Viney (Guest)
on 2006-03-10 10:23
(Received via mailing list)
This plugin gives ActiveRecord the ability to do stricter date checking.
Example:

  class Person < ActiveRecord::Base
    validates_date :date_of_birth
  end

This will ensure that date_of_birth is a *valid* date. The date can be
set
initially as a string in any of the following formats:

  "2006-01-01"
  "1 Jan 06", or "1 Jan 2006"
  "1/1/06"  # Day / Month / Year

They will all be parsed into a date object before the model is saved. If
the
date has been set to an invalid string, eg "Meow", an error is added to
the
model. Example (using Person as defined above):

p = Person.new
p.date_of_birth = "1 Jan 2006"
p.save  # true

p.date_of_birth = "java is better than ruby"
p.save  # false

In the final example, as I'm sure you are aware, the record failed to
save
not only because "java is better than ruby" is an invalid date, but more
importantly, because the statement is blatantly false. ;)

Install with:

script/plugin install
http://svn.viney.net.nz/things/rails/plugins/validates_date


Check it's all working with:

rake test:plugins [or rake test_plugins if you are using Rails 1.0]

Suggestions, comments, problems are all welcome.

-Jonathan.
92dafea96df3ff949b426b235a19ae0e?d=identicon&s=25 ChrisT (Guest)
on 2006-03-10 10:54
(Received via mailing list)
Does it check re days of the moth -- i.e. would 30 Feb throw an error?
Chris T
B07fcd81d491ce2209d7995c4a851f9a?d=identicon&s=25 Jonathan Viney (Guest)
on 2006-03-10 11:06
(Received via mailing list)
Yes it does.

>> p = Person.find(:first)
>> p.date_of_birth = '30 feb 06'
>> p.save
=> false

>> p.date_of_birth = '28 feb 06'
>> p.save
=> true

-Jonathan.
59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2006-03-10 16:45
(Received via mailing list)
Does it take Ruby Date and/or Time objects in addition to strings?

--
-- Tom Mornini
B07fcd81d491ce2209d7995c4a851f9a?d=identicon&s=25 Jonathan Viney (Guest)
on 2006-03-10 23:24
(Received via mailing list)
It works with Date objects, probably not Time though. It could be made
to,
but I wanted this for working with dates. I can possibly add an extra
validator such as validates_datetime ...

-Jonathan
B07fcd81d491ce2209d7995c4a851f9a?d=identicon&s=25 Jonathan Viney (Guest)
on 2006-03-11 06:27
(Received via mailing list)
I've just added a validates_time method in as well. Same principle as
validates_date:

  class Person < ActiveRecord::Base
    validates_time :time_of_birth
  end

  p = Person.new
  p.time_of_birth = '5am'
  p.save  # true

  p.time_of_birth = 'ceiling'
  p.save  # false

Currently accepted formats are things like:

  1am
  12 30
  3:30PM
  7pm

I'm using this in an application I'm developing at the moment, so there
may
be more additions coming if I need them.

-Jonathan
5bf65e262057f11fe1c3e7b8a7fa1b1f?d=identicon&s=25 bkj123@gmail.com (Guest)
on 2006-03-11 14:05
(Received via mailing list)
Hello

New to programming and Ruby.  I want to create an application that would
be installed on a windows machine.  It will hook to a unix server via
ftp and display all the files on the server.  It will also have an
editing window to edit and create new documents and save them to the
unix box all while the app is on the windows machine.  A similar app is
FTPEditor.

Can I use Ruby to develop such an application.  I would like for the app
to be a very easy install and not insist that the user also have a
webserver installed (apache) and what not.   Thoughts ?
49e98c4a13a5203ecff5d2d0774f8991?d=identicon&s=25 Matthijs Langenberg (Guest)
on 2006-03-11 14:08
(Received via mailing list)
Perhaps you have the wrong idea about Rails. It's a framework to create
web
applications with, not win32 applications.
A05834e9b5954947eb0ba3b570c47d5e?d=identicon&s=25 Pratik Naik (pratik)
on 2006-03-11 14:20
(Received via mailing list)
It's quite possible doing this with Rails. Basically you'll need to
develop web based ftp client and text editor.

Thanks,
Pratik
143d37bc67b04ad4fdc69beddda48651?d=identicon&s=25 Tom Taylor (Guest)
on 2006-03-11 14:35
(Received via mailing list)
Jonathan Viney wrote:
> This plugin gives ActiveRecord the ability to do stricter date checking.
> Example:
>
>   class Person < ActiveRecord::Base
>     validates_date :date_of_birth
>   end

Brilliant, thanks a lot! Will come in very useful - perhaps should be
integrated into core rails?

Tom
143d37bc67b04ad4fdc69beddda48651?d=identicon&s=25 Tom Taylor (Guest)
on 2006-03-11 14:41
(Received via mailing list)
Tom Taylor wrote:
> Brilliant, thanks a lot! Will come in very useful - perhaps should be
> integrated into core rails?

That said - I'm getting a 404 on the file...

Tom
B07fcd81d491ce2209d7995c4a851f9a?d=identicon&s=25 Jonathan Viney (Guest)
on 2006-03-11 16:07
(Received via mailing list)
Ah, yes, I changed the name to validates_date_time when I added the time
validation.

  script/plugin install
http://svn.viney.net.nz/things/rails/plugins/valid...

The readme should tell you all you need to know. If you want to see the
expected behaviour just have a look at the tests, should be pretty
self-explanatory.

-Jonathan.
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2006-03-11 17:13
Does it validate leap years?

Jeff Coleman
B07fcd81d491ce2209d7995c4a851f9a?d=identicon&s=25 Jonathan Viney (Guest)
on 2006-03-11 22:32
(Received via mailing list)
You mean dates like 29 Feb 2004? Yes.

Just install the plugin and have a play.

-Jonathan.
91a469d9f43a066babfd5531311d0fd1?d=identicon&s=25 Rath --- (rath)
on 2006-03-12 03:00
Hi John,

What about dates previous to 1970? Because at the moment I haven't been
able to validate dates before that year or do you know how can I do it?
Matz mentioned it in another ruby forum that the dates need to be
changed from Time to Date or the other way around... don't remember but
I don't know how to implement that. Could you help he here.

Thanks,

Rafa

Jonathan Viney wrote:
> You mean dates like 29 Feb 2004? Yes.
>
> Just install the plugin and have a play.
>
> -Jonathan.
B07fcd81d491ce2209d7995c4a851f9a?d=identicon&s=25 Jonathan Viney (Guest)
on 2006-03-12 03:58
(Received via mailing list)
Yes, validates_date will handle any dates that the Ruby Date class can
handle, which includes dates back past 1970.

-Jonathan.
631d1677786f0725da6ccee46043686e?d=identicon&s=25 Jonathan Viney (Guest)
on 2006-03-21 00:20
(Received via mailing list)
I've just added the ability restrict the date to a given range. You can
now
do:

  class Person < ActiveRecord::Base
    validates_date :date_of_birth, :before => Proc.new {
1.day.from_now.to_date }, :after => Proc.new { Date.new(1900, 1, 1) }
  end

The default behaviour has also changed to restrict the initial range to
1900
=> 1 year from now. To override that pass :before => nil, or :after =>
nil.

Grab it from
http://svn.viney.net.nz/things/rails/plugins/valid... if
you're
interested.

Cheers, Jonathan.
E74a88a96ce8caecbc7d265e4763e26c?d=identicon&s=25 Stuart Rackham (Guest)
on 2006-03-22 22:00
On the same topic, here's a date validator I posted to Code Snippets
which accepts a wide range of user friendly date formats:

http://www.bigbold.com/snippets/posts/show/1548


Cheers, Stuart



Jonathan Viney wrote:
> This plugin gives ActiveRecord the ability to do stricter date checking.
> Example:
>
>   class Person < ActiveRecord::Base
>     validates_date :date_of_birth
>   end
>
> This will ensure that date_of_birth is a *valid* date. The date can be
> set
> initially as a string in any of the following formats:
>
>   "2006-01-01"
>   "1 Jan 06", or "1 Jan 2006"
>   "1/1/06"  # Day / Month / Year
>
> They will all be parsed into a date object before the model is saved. If
> the
> date has been set to an invalid string, eg "Meow", an error is added to
> the
> model. Example (using Person as defined above):
>
> p = Person.new
> p.date_of_birth = "1 Jan 2006"
> p.save  # true
>
> p.date_of_birth = "java is better than ruby"
> p.save  # false
>
> In the final example, as I'm sure you are aware, the record failed to
> save
> not only because "java is better than ruby" is an invalid date, but more
> importantly, because the statement is blatantly false. ;)
>
> Install with:
>
> script/plugin install
> http://svn.viney.net.nz/things/rails/plugins/validates_date
>
>
> Check it's all working with:
>
> rake test:plugins [or rake test_plugins if you are using Rails 1.0]
>
> Suggestions, comments, problems are all welcome.
>
> -Jonathan.
Befa945ac6d3bf13c4b41657e8558f85?d=identicon&s=25 Mike Dershowitz (dersh53)
on 2007-12-21 20:53
Hi:

Validates_date is great!  But I'm getting an error when using the
"after" parameter:

  validates_date :birthdate, :message => 'is not a valid date',:before
=> Proc.new {(Time.now - 10.years).to_date}, :after => Proc.new
{(Time.now - 150.years).to_date}, :if => Proc.new{|profile|
(profile.attributes_before_type_cast["birthdate"] &&
profile.attributes_before_type_cast["birthdate"] != "") }

Gives this error:

"invalid restriction"
(eval):32:in `date_meets_relative_restrictions'
(eval):14:in `select'
(eval):14:in `date_meets_relative_restrictions'
(eval):31:in `validates_date'

Can you help?

Mike
631d1677786f0725da6ccee46043686e?d=identicon&s=25 Jonathan Viney (jviney)
on 2007-12-23 05:06
(Received via mailing list)
You are using an old version of the plugin. Try updating and let me
know is that fixes your problem.

-Jonathan.

On Dec 22, 8:53 am, Mike Dershowitz <rails-mailing-l...@andreas-s.net>
This topic is locked and can not be replied to.