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 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Nov 3, 2007, at 10:59 AM, Thiago Guerra wrote:> 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." > endHave a look at this info here: http://snippets.dzone.com/posts/show/3048 -- gw --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 3 Nov 2007, at 17:59, Thiago Guerra wrote:> > 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> 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 > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
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 On Nov 3, 3:12 pm, Greg Willits <li...-0Bv1hcaDFPRk211Z5VL+QA@public.gmane.org> wrote:> On Nov 3, 2007, at 10:59 AM, Thiago Guerra wrote: > > > 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 > > Have a look at this info here: > > http://snippets.dzone.com/posts/show/3048 > > -- gw--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Apparently Analagous Threads
- calculate users age
- validates_numericality_of with greater_than* less_than* simply don't work
- ActiveRecord: When / where to validate data? Tricky question
- validates_numericality_of, :allow_nil => true?
- At my wits end ! Controlling passed in negative values from a form