Validating date_select properly

Hi,

I’m trying to validate a date_select field. :include_blank is set to
true, and the default value is obviously blank on a new object.

Unfortunately, if a user only selects a day, and leaves the month and
year blank, then the date is accepted with default values of January
and 1901, respectively.

I’d rather not do any validation in the controller, but that’s the
only place I can think of doing it ( checking the validity of the date
based on the individual components dob(2i) and dob(1i) ).

I’m also having problems checking for the validity of the dates
themselves. If I try a date of, say, February 30th, Actioncontroller
just craps out with “1 error(s) on assignment of multiparameter
attributes”. It doesn’t get to stage of validating the date from the
model. How can I get around this? I’d rather not present my users with
a “PROGRAM BROKE!” error. :slight_smile:

Dave


Site: http://antidis.com/

Anyone?

On 11/22/05, David B. [email protected] wrote:

only place I can think of doing it ( checking the validity of the date


Site: http://antidis.com/


Site: http://antidis.com/

How about in the model’s validate method?


– Tom M.

That’s what I’m trying, but:

a) If any date component (day, month or year) is received, those
values that have not been provided move to defaults. I want to know
whether or not the values have been received.
b) If I provide a date which is nonsense in our calendar (30th of
February for example), I get an Actioncontroller crap-out.

Dave

On 11/23/05, Tom M. [email protected] wrote:

On 11/22/05, David B. [email protected] wrote:

only place I can think of doing it ( checking the validity of the

Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Site: http://antidis.com/

On 11/23/05, David B. [email protected] wrote:

That’s what I’m trying, but:

a) If any date component (day, month or year) is received, those
values that have not been provided move to defaults. I want to know
whether or not the values have been received.
b) If I provide a date which is nonsense in our calendar (30th of
February for example), I get an Actioncontroller crap-out.

In other words, it doesn’t get to the model.

and 1901, respectively.
model. How can I get around this? I’d rather not present my users


Site: http://antidis.com/


Site: http://antidis.com/

Hi David,

I don’t think this is necessarily a bug but rather a short-coming of the
date select method which is really a helper method. I think it would be
better to document the shortcomings of the date select method.

The birthdate member of the model is a Date object. When it is
initialized
it expects valid parameters which are coming from the date selecter.
This
initialization happens before the validation phase so in my opinion it
is
not really a rails bug.

Your solution is a valid one but MultiparameterAssignmentErrors might
not
always be due to an invalid birth date, though 99% of the time it is.
And
when it does happen you will find that the user might be confused by the
error message since his birth date input would have been changed to a
different date.

I think if you really want to do things “correctly” you would have
separate
members for month, day, and year and then validation would check these
fields and a custom validation rule would check that the date is valid.
Then after validation but before save you would populate the date field
using these members. It’s a bit convoluted and I haven’t done it myself
but
instead just ended up catching the error in the controller which isn’t
very
satisfactory.

That’s just my opinion.
-Frank

Fixed it:
http://convergentarts.com/articles/2005/06/25/validating-a-date

Should this be classed as a bug?

Dave

On 11/23/05, David B. [email protected] wrote:

and 1901, respectively.
model. How can I get around this? I’d rather not present my users


Site: http://antidis.com/


Site: http://antidis.com/


Site: http://antidis.com/