# Validating date\_select invalid dates

**URL:** <https://www.ruby-forum.com/t/validating-date-select-invalid-dates/122109>\
**Category:** Rails\
**Created:** [December 4, 2007, 5:25pm UTC](https://www.ruby-forum.com/t/validating-date-select-invalid-dates/122109 "2007-12-04T17:25:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thiago\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19b73/32.png) [@Thiago\_G](https://www.ruby-forum.com/u/Thiago_G)\
**Post date:** [December 4, 2007, 5:25pm UTC](https://www.ruby-forum.com/t/validating-date-select-invalid-dates/122109/1 "2007-12-04T17:25:54Z")

</div>

Hi,

I’m trying to validate a invalid date like Feb 31, 1976 but I’m  
getting a really weird error msg and I can’t figure out how to fix it.  
I thought that using validates\_multiparameter\_assignments it would  
handle any invalid date posted but it is not working. Everytime that I  
try I get:

You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.\>

#{RAILS\_ROOT}/app/models/spec.rb:19  
#{RAILS\_ROOT}/app/controllers/spec\_controller.rb:16:in `edit' /Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/i386/ bin/mongrel_rails:16:in`load’  
/Applications/Locomotive2/Bundles/standardRailsMar2007.locobundle/i386/  
bin/mongrel\_rails:16

but birthdate is not nil!!

If I try with a valid date everything will work perfectly.

below I’m pasting my controller and view code

class Spec \< ActiveRecord::Base

belongs\_to :user  
acts\_as\_ferret

ALL\_FIELDS = %w(first\_name last\_name address phone gender birthdate  
city state zip\_code)  
STRING\_FIELDS = %w(first\_name last\_name address phone city state)  
VALID\_GENDERS = [“Male”, “Female”]  
START\_YEAR = 1900  
END\_YEAR = Date.today.year - 18  
ZIP\_CODE\_LENGTH = 10

validates\_presence\_of :zip\_code, :birthdate, :first\_name, :last\_name,  
:phone, :city, :state, :country  
validates\_length\_of STRING\_FIELDS, :maximum =\> DB\_STRING\_MAX\_LENGTH  
validates\_inclusion\_of :gender, :in =\> VALID\_GENDERS, :allow\_nil =\>  
true, :message =\> “must be male or female”  
validates\_multiparameter\_assignments :message =\> " is not a valid  
date."  
validates\_each :birthdate do |record, attr, value|  
puts value.class  
record.errors.add attr, “is not a valid date. You must be at  
least 18 years old to sign in.” if value \> Date.new((Date.today.year -  
18),(Date.today.month),(Date.today.day))  
end  
end

\<% form\_for :spec do |form| %\>

 \<%= "edit #{@user.screen\_name} profile" %\>

\<%= error\_messages\_for ‘spec’ %\>  
\<%= field\_for(“text”, form, “first\_name”)%\>  
\<%= field\_for(“text”, form, “last\_name”) %\>

  
Gender:  
\<%= radio\_button(:spec, :gender, “Male”) %\> Male  
\<%= radio\_button(:spec, :gender, “Female”) %\> Female  

  

  
Birthdate:  
\<%= date\_select :spec, :birthdate,  
:start\_year =\> Spec::END\_YEAR,  
:end\_year =\> Spec::START\_YEAR,  
:order =\> [:month, :day, :year] %\>  

  
\<%= field\_for(“text”, form, “phone”) %\>  
\<%= field\_for(“text”, form, “address”) %\>  
\<%= field\_for(“text”, form, “city”) %\>  
\<%= field\_for(“text”, form, “state”) %\>  
\<%= field\_for(“text”, form, “country”) %\>  
\<%= field\_for(“text”, form, “zip\_code”, Spec::ZIP\_CODE\_LENGTH,  
Spec::ZIP\_CODE\_LENGTH) %\>

```
<%= submit_tag "Update", :class => "submit" %>

```

\<% end %\>

Thanks,  
Thiago G.

---

<div class="post-metadata">

**Author:** ![Thiago\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19b73/32.png) [@Thiago\_G](https://www.ruby-forum.com/u/Thiago_G)\
**Post date:** [January 24, 2008, 12:13pm UTC](https://www.ruby-forum.com/t/validating-date-select-invalid-dates/122109/2 "2008-01-24T12:13:48Z")

</div>

I’m having the same problem! My date\_select(‘days’ field returns  
standard 31 days) in the view can return also invalid dates like 31  
februari. There is a plugin to have it return an errormessage

[http://agilewebdevelopment.com/plugins/validates\_multiparameter\_assignments](http://agilewebdevelopment.com/plugins/validates_multiparameter_assignments)

But that’s totally not what I want and how it should be, is it?

I want the invalid dates to dissapear! Anyone know a solution?
