[ANN] validates_date plugin for ActiveRecord


#1

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. :wink:

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.


#2

Does it check re days of the moth – i.e. would 30 Feb throw an error?
Chris T


#3

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.


#4

Does it take Ruby Date and/or Time objects in addition to strings?


– Tom M.


#5

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


#6

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


#7

Perhaps you have the wrong idea about Rails. It’s a framework to create
web
applications with, not win32 applications.


#8

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 ?


#9

Jonathan V. 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


#10

Tom T. 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


#11

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/validates_date_time

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.


#12

Does it validate leap years?

Jeff C.man


#13

You mean dates like 29 Feb 2004? Yes.

Just install the plugin and have a play.

-Jonathan.


#14

It’s quite possible doing this with Rails. Basically you’ll need to
develop web based ftp client and text editor.

Thanks,
Pratik


#15

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 V. wrote:

You mean dates like 29 Feb 2004? Yes.

Just install the plugin and have a play.

-Jonathan.


#16

Yes, validates_date will handle any dates that the Ruby Date class can
handle, which includes dates back past 1970.

-Jonathan.


#17

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/validates_date_time if
you’re
interested.

Cheers, Jonathan.


#18

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:inselect’
(eval):14:in date_meets_relative_restrictions' (eval):31:invalidates_date’

Can you help?

Mike


#19

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 D. removed_email_address@domain.invalid


#20

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 V. 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. :wink:

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.