Date_select and "31st February"


#1

Hi,

I’m writing a form where the user should enter their date of birth.

Here’s the code:

<% form_for :applicant, :url=> {:action => “index”} do |f| %>

<%= f.label :dob, “Date of Birth” %>
<%= f.date_select :dob, :include_blank => true %>

<%= submit_tag "Submit" %>

<% end %>

When I open the form in my browser everything is fine.
However when I select “February” as the month, “31” as the day, don’t
select a year, and then press “Submit” I get the following error
message:

1 error(s) on assignment of multiparameter attributes

Can anyone tell me why this is happening and how to avoid this?

Thanks in advance.


#2

On 3 Apr 2009, at 13:38, Jim B. wrote:

<%= f.date_select :dob, :include_blank => true %>

Can anyone tell me why this is happening and how to avoid this?

because rails is just faithfully passing this down and trying to
create a date object for 31st february.

update_attributes, Applicant.new etc. will raise
ActiveRecord::MultiparameterAssignmentErrors when this happens.

Fred


#3

On 3 Apr 2009, at 14:02, Jim B. wrote:

Thanks for the quick reply.
It is important for my form that the date_select helper is not
initialized with a value (ie. :include_blank => true).
So, how could I stop the above error from happening?

You can’t easily stop the user entering dates like that with rails
built in date helpers, so you need to rescue those exceptions and
massage them into a friendly message.

Fred


#4

Thanks for the quick reply.
It is important for my form that the date_select helper is not
initialized with a value (ie. :include_blank => true).
So, how could I stop the above error from happening?
Could you give me a hint as to which direction to be thinking in?
I’m still relatively new to Rails, so I would be grateful if any
explination isn’t too complicated.
Thanks once again.


#5

On Apr 3, 4:29 pm, Jim B. removed_email_address@domain.invalid
wrote:

You can’t easily stop the user entering dates like that with rails
built in date helpers, so you need to rescue those exceptions and
massage them into a friendly message.

Hi Fred,
I’m quite surprised, as I would have thought that this makes the entire
date_select helper open to misuse.

date_select is simple & stupid. Seems to me like the worst case is
someone enters an invalid date and the create fails.

Nonetheless, two further questions:

How does one rescue exceptons in rails? I just need a point to start.

http://www.rubycentral.com/pickaxe/tut_exceptions.html

Would it be a feasible option to use three text fields in the form that
are unrelated to the model. Then in the controller string them together
and make a date object? (that would be more or less how I would do it in
PHP).

You could. You’d still have to rescue the exception that is raised by
Date::civil when you pass it 2009,2,29 (or do all the bounds checking
yourself)

Fred


#6

You can’t easily stop the user entering dates like that with rails
built in date helpers, so you need to rescue those exceptions and
massage them into a friendly message.

Hi Fred,
I’m quite surprised, as I would have thought that this makes the entire
date_select helper open to misuse.
Nonetheless, two further questions:

How does one rescue exceptons in rails? I just need a point to start.

Would it be a feasible option to use three text fields in the form that
are unrelated to the model. Then in the controller string them together
and make a date object? (that would be more or less how I would do it in
PHP).


#7

Just thought I’d let you know how things turned out.

I have discarded the idea of using date_select as it is full of
inconsistencies which in the worst case can cause the application to
crash.
Instead I created three seperate text fields (DD-MM-YYYY)and using
virtual attributes in the model mapped them to the “dob” field in the
db.
Now everything works like it should do (at last).

I am surprised that this has presented me with such a problem as
entering one’s dob into a form is a very common occurence.

Nonetheless thanks for your help Fred. You definitely pointed me in the
right direction.