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| %> <fieldset> <legend><%= "edit #{@user.screen_name} profile" %></legend> <%= error_messages_for ''spec'' %> <%= field_for("text", form, "first_name")%> <%= field_for("text", form, "last_name") %> <div class="form_row"> <label for="gender">Gender:</label> <%= radio_button(:spec, :gender, "Male") %> Male <%= radio_button(:spec, :gender, "Female") %> Female </div> <div class="form_row"> <label for="birthdate">Birthdate:</label> <%= date_select :spec, :birthdate, :start_year => Spec::END_YEAR, :end_year => Spec::START_YEAR, :order => [:month, :day, :year] %> </div> <%= 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" %> </fieldset> <% end %> Thanks, Thiago Guerra --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
kidxrude-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Jan-24 11:12 UTC
Re: Validating date_select invalid dates
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? On 4 dec 2007, 17:25, Thiago Guerra <thiag...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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| %> > <fieldset> > <legend><%= "edit #...@user.screen_name} profile" %></legend> > > <%= error_messages_for ''spec'' %> > <%= field_for("text", form, "first_name")%> > <%= field_for("text", form, "last_name") %> > <div class="form_row"> > <label for="gender">Gender:</label> > <%= radio_button(:spec, :gender, "Male") %> Male > <%= radio_button(:spec, :gender, "Female") %> Female > </div> > <div class="form_row"> > <label for="birthdate">Birthdate:</label> > <%= date_select :spec, :birthdate, > :start_year => Spec::END_YEAR, > :end_year => Spec::START_YEAR, > :order => [:month, :day, :year] %> > </div> > <%= 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" %> > </fieldset> > <% end %> > > Thanks, > Thiago Guerra--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---