[Rails] date validation
Marcel Molina Jr.
marcel at vernix.org
Wed Mar 2 20:06:16 GMT 2005
On Wed, Mar 02, 2005 at 12:11:46PM -0700, Nathan Wright wrote:
> I'm hoping someone can help me (a Rails newbie) with this. Basically, I
> need to verify that a date created with "date_select" is valid *before*
> it's sent off to the database for acceptance (as a valid date) or
> rejection (as an invalid date). Given how easy the majority of validation
> are in Rails, I've got to believe there's a fairly simple way to do this,
> but for the life of me I can't find it.
>
> At this point, I'm even ready to accept a difficult solution, as long as
> it's a solution. Any ideas would be greatly appreciated.
There is a method in AR::Base called
execute_callstack_for_multiparameter_attributes. It has a line that reads:
send(name + "=", Time == klass ? klass.local(*values) : klass.new(*values))
If someone submits February 30 in a form helper then Time.local gets
passed an invalid date which makes it raise an exception. This happens
before the validation callbacks so you can not protect yourself from it,
nor do I even think there is a way from outside of AR itself to wrap
this in an exception.
Though I would concede to whomever would be of the position that an
invalid date is an exceptional state, I'd rather be able to catch this in
AR validations and report it to the user then have my app bomb out. I
set out to patch this a while ago but didn't come up with anything
satisfactory because of where this method is defined and how it is
called. Granted I only spent a few hours on it. I would be psyched if
someone made this something that validations can catch.
marcel
--
Marcel Molina Jr. <marcel at vernix.org>
More information about the Rails
mailing list