Birthdate validation

Hello everyone,

I'm almost cracking my head trying to do this birthdate validation. It turns out that I can only accept users with at least 18 years old and I'm trying to validate it writing this code on my user.rb fil at app/ models

class User < ActiveRecord::Base   validates_presence_of :full_name   validates_presence_of :street_address   validates_presence_of :city   validates_presence_of :state   validates_presence_of :country   validates_presence_of :zip_code   validates_numericality_of :zip_code, :message => "must contain only numbers."   validates_presence_of :phone   validates_numericality_of :phone, :message => "must contain only numbers."   validates_presence_of :email   validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a- z]{2,})$/i   validates_acceptance_of :policy_agreement, :message => "must be accepted. You have to read and agree with the Aykall Policies before signing in."   validates_multiparameter_assignments :message => " is not a valid date."   age_limit = Date.new(Date.today.year, Date.today.month, Date.today.day).change(:year => Date.today.year - 18)   validates_numericality_of :birth_date, :less_than => age_limit, :message => "is invalid. You have to be at least 18 years old to sign in." end

I'm getting the error message "undefined method `change' for #<Date: 4908815/2,0,2299161>" but, honestly, I don't know what else to try.

Can anyone help me?

Thanks, Thiago Guerra

Have a look at this info here:

http://snippets.dzone.com/posts/show/3048

-- gw

Hello everyone,

I'm almost cracking my head trying to do this birthdate validation. It turns out that I can only accept users with at least 18 years old and I'm trying to validate it writing this code on my user.rb fil at app/ models

  age_limit = Date.new(Date.today.year, Date.today.month, Date.today.day).change(:year => Date.today.year - 18)   validates_numericality_of :birth_date, :less_than => age_limit, :message => "is invalid. You have to be at least 18 years old to sign in." end

change only exists on time, not date. Regardless this won't work
properly: the above is only evaluated when the code is first loaded,
so say you start up your app on the first of december 2007 it would
check that they're born before 1st december 1989. A month later it
will check against that date of first of december. I think you'd be far better off with a custom validation method.

Fred

Thanks man.

It worked with:

  validates_multiparameter_assignments :message => " is not a valid date."   validates_each :birth_date do |record, attr, value|       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