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.
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 …
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 ?
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.
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.
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.
On the same topic, here’s a date validator I posted to Code Snippets
which accepts a wide range of user friendly date formats:
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.