Validating date_select invalid dates

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:inload’
/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.

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

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?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs