date_select and "31st February"

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 %>
..
<p><%= submit_tag "Submit" %></p>
<% 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.

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 %>
..
<p><%= submit_tag "Submit" %></p>
<% 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?

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

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

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).

> 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
yourself)

Fred