I use date_select on my sign_up sheet for people to choose their birthdate. For people to join the website they have to be over 18. How would I go about validating that in a rails way. For all the other validations I got them. I just can''t figure out how to do this one yet. Any suggestions? :-)
Just add a validate member to the ActiveRecord::Base decendant class that has birthdate field. Inside chegk if they are 18 or above. If they are not add a desciptive message to the errors field. Something roughly looking like: def validate if Time.now - birthdate < 18.years errors.add("birthdate", "must be over 18") end end Zsombor On 5/26/05, John Kopanas <john.kopanas-O1KSuMybMhqBUy7/sJONFg@public.gmane.org> wrote:> I use date_select on my sign_up sheet for people to choose their > birthdate. For people to join the website they have to be over 18. > How would I go about validating that in a rails way. For all the > other validations I got them. I just can''t figure out how to do this > one yet. > > Any suggestions? :-) > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- http://deezsombor.blogspot.com
Wow... thanks... makes sense. But I am really new to both rails and ruby and have a couple questions about your solution if you don''t mind. Using the combination of what you wrote and: http://wiki.rubyonrails.com/rails/show/HowToAddCustomValidationKeywords I want to make a validate_older_then_18 so I can write in my model: validate_older_then_18 :birthdate Where would I extend the ActiveRecord::Base class? What file do I put this stuff in? Thanks my friend. On 26-May-05, at 8:20 AM, Dee Zsombor wrote:> Just add a validate member to the ActiveRecord::Base decendant class > that has birthdate field. Inside chegk if they are 18 or above. If > they are not add a desciptive message to the errors field. Something > roughly looking like: > > def validate > if Time.now - birthdate < 18.years > errors.add("birthdate", "must be over 18") > end > end > > Zsombor > > On 5/26/05, John Kopanas <john.kopanas-O1KSuMybMhqBUy7/sJONFg@public.gmane.org> wrote: > >> I use date_select on my sign_up sheet for people to choose their >> birthdate. For people to join the website they have to be over 18. >> How would I go about validating that in a rails way. For all the >> other validations I got them. I just can''t figure out how to do this >> one yet. >> >> Any suggestions? :-) >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > > > -- > http://deezsombor.blogspot.com > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
I think I am making things more complicated then need be for the time being. In the User model I just added: def before_save if Time.now - birthdate < 18.years errors.add("birthdate", "must be over 18") end end but fore some reason before_save is not being called before_save... or should I be declaring before_save in another file? Do I need to specify object before errors like User.errors.add? Your guys help is greatly appreciated! On 26-May-05, at 9:06 AM, John Kopanas wrote:> Wow... thanks... makes sense. But I am really new to both rails > and ruby and have a couple questions about your solution if you > don''t mind. > > Using the combination of what you wrote and: > http://wiki.rubyonrails.com/rails/show/ > HowToAddCustomValidationKeywords > > I want to make a validate_older_then_18 so I can write in my model: > > validate_older_then_18 :birthdate > > Where would I extend the ActiveRecord::Base class? What file do I > put this stuff in? > > Thanks my friend. > > > On 26-May-05, at 8:20 AM, Dee Zsombor wrote: > > >> Just add a validate member to the ActiveRecord::Base decendant class >> that has birthdate field. Inside chegk if they are 18 or above. If >> they are not add a desciptive message to the errors field. Something >> roughly looking like: >> >> def validate >> if Time.now - birthdate < 18.years >> errors.add("birthdate", "must be over 18") >> end >> end >> >> Zsombor >> >> On 5/26/05, John Kopanas <john.kopanas-O1KSuMybMhqBUy7/sJONFg@public.gmane.org> wrote: >> >> >>> I use date_select on my sign_up sheet for people to choose their >>> birthdate. For people to join the website they have to be over 18. >>> How would I go about validating that in a rails way. For all the >>> other validations I got them. I just can''t figure out how to do >>> this >>> one yet. >>> >>> Any suggestions? :-) >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >>> >>> >> >> >> -- >> http://deezsombor.blogspot.com >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
> Using the combination of what you wrote and: > http://wiki.rubyonrails.com/rails/show/ > HowToAddCustomValidationKeywords > > I want to make a validate_older_then_18 so I can write in my model: > > validate_older_then_18 :birthdate > > Where would I extend the ActiveRecord::Base class? What file do I > put this stuff in?That method should probably be called "validates_older_than_18". In addition, I''d make the 18 a parameter instead so you could re-use it elsewhere: validates_older_than :birthdate, :age => 18 module ActiveRecord module Validations module ClassMethods def validates_older_than(*attr_names) configuration = { :message => "must be over %d" } configuration.update(attr_names.pop) if attr_names.last.is_a? (Hash) min_age = configuration[:age].to_i validate_each (attr_names, configuration) do |record, attr_name, value| record.errors.add(attr_name, configuration[:message] % min_age) unless Time.now - value < 18.years end end end end end _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Wow... where would I put this? On 26-May-05, at 10:07 AM, Duane Johnson wrote:>> Using the combination of what you wrote and: >> http://wiki.rubyonrails.com/rails/show/ >> HowToAddCustomValidationKeywords >> >> I want to make a validate_older_then_18 so I can write in my model: >> >> validate_older_then_18 :birthdate >> >> Where would I extend the ActiveRecord::Base class? What file do I >> put this stuff in? > > That method should probably be called "validates_older_than_18". > In addition, I''d make the 18 a parameter instead so you could re- > use it elsewhere: > > validates_older_than :birthdate, :age => 18 > > module ActiveRecord > module Validations > module ClassMethods > def validates_older_than(*attr_names) > configuration = { :message => "must be over %d" } > configuration.update(attr_names.pop) if > attr_names.last.is_a?(Hash) > min_age = configuration[:age].to_i > validate_each (attr_names, configuration) do |record, > attr_name, value| > record.errors.add(attr_name, configuration[:message] % > min_age) unless Time.now - value < 18.years > end > end > end > end > end > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
This snippet has a small bug: 18.years is still hard coded, you should write age.years. Also I would inspect if generalization is worthwhile at all ... do you have a constraint at application level for people under 23 years? Or perhaps I''m just too lazy doing framework stuff ;-)> validates_older_than :birthdate, :age => 18 > > module ActiveRecord > module Validations > module ClassMethods > def validates_older_than(*attr_names) > configuration = { :message => "must be over %d" } > configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) > min_age = configuration[:age].to_i > validate_each (attr_names, configuration) do |record, attr_name, > value| > record.errors.add(attr_name, configuration[:message] % min_age) > unless Time.now - value < 18.years > end > end > end > end > end-- http://deezsombor.blogspot.com
Right now I only have constraint for 18... but who knows. Thanks for pointing out that error though. On 26-May-05, at 10:32 AM, Dee Zsombor wrote:> This snippet has a small bug: 18.years is still hard coded, you should > write age.years. Also I would inspect if generalization is worthwhile > at all ... do you have a constraint at application level for people > under 23 years? Or perhaps I''m just too lazy doing framework stuff ;-) > > >> validates_older_than :birthdate, :age => 18 >> >> module ActiveRecord >> module Validations >> module ClassMethods >> def validates_older_than(*attr_names) >> configuration = { :message => "must be over %d" } >> configuration.update(attr_names.pop) if >> attr_names.last.is_a?(Hash) >> min_age = configuration[:age].to_i >> validate_each (attr_names, configuration) do |record, >> attr_name, >> value| >> record.errors.add(attr_name, configuration[:message] % >> min_age) >> unless Time.now - value < 18.years >> end >> end >> end >> end >> end >> > > -- > http://deezsombor.blogspot.com > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
> Wow... where would I put this?I would put it in some file such as "activerecord_extensions.rb" (or something that makes sense to you) and keep it in the lib folder of your rails application. Then add this to your config/environment.rb file (at the end): require ''activerecord_extensions.rb'' Rails will know how to find it automatically, so there''s no need to specify the directory. Requiring it like this in your environment.rb file will guarantee that all parts of your application have access to it (i.e. it guarantees that the modification to ActiveRecord will occur before your models are auto-included). Duane Johnson (canadaduane) _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
John Kopanas <john.kopanas-O1KSuMybMhqBUy7/sJONFg@public.gmane.org> writes:> How would I go about validating that in a rails way.My solution is like this: validates_inclusion_of :dob, :in => Date.new(1930)..18.years.ago.to_date, :message => "Must be born between 1930 and #{18.years.ago.strftime(''%Y'')}" The 1930 thing is kind of arbitrary though, but this doesn''t require extending AR. -- doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org
Works great. I set the date arbitrarily to 1899 and make sure my pulldown menus don''t have anything less then 1900 so I don''t have to write in the copy anything about a max age. I also in my date_select don''t put any years where I am guaranteed to have people under 18. This way there is a low chance of someone selecting a date where they are younger then 18: date_select("user", "birthdate", :start_year => 1900, :end_year => Date.today.year - 18) Out of curiosity. How did you verify the date they did choose was not an invalid date? I am having problems with that because we have to verify it before the object gets created. On 26-May-05, at 2:51 PM, Doug Alcorn wrote:> John Kopanas <john.kopanas-O1KSuMybMhqBUy7/sJONFg@public.gmane.org> writes: > > >> How would I go about validating that in a rails way. >> > > My solution is like this: > > validates_inclusion_of :dob, :in => Date.new(1930).. > 18.years.ago.to_date, :message => "Must be born between 1930 and # > {18.years.ago.strftime(''%Y'')}" > > The 1930 thing is kind of arbitrary though, but this doesn''t require > extending AR. > -- > doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Come to think of it there is a problem. <%= 18.years.ago.to_date %> Displays the correct year and month but gives me the last day of the month. Hence you are 18 soon as you enter into the month which legally is not true :-). Anyone know why? On 26-May-05, at 2:51 PM, Doug Alcorn wrote:> John Kopanas <john.kopanas-O1KSuMybMhqBUy7/sJONFg@public.gmane.org> writes: > > >> How would I go about validating that in a rails way. >> > > My solution is like this: > > validates_inclusion_of :dob, :in => Date.new(1930).. > 18.years.ago.to_date, :message => "Must be born between 1930 and # > {18.years.ago.strftime(''%Y'')}" > > The 1930 thing is kind of arbitrary though, but this doesn''t require > extending AR. > -- > doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
validates_inclusion_of :birthdate, :in => Date.new(1900).. 18.years.ago.to_date, :message => "Hey... you are too young... wait until you turn 18 at least." 18.years.ago.to_date => 1987-06-06 I entered the date: 1978-01-01 00:00:00 -05:00 and I get an error thrown saying that it does not fall under those two dates. Does anyone know why this is happening? On 26-May-05, at 2:51 PM, Doug Alcorn wrote:> John Kopanas <john.kopanas-O1KSuMybMhqBUy7/sJONFg@public.gmane.org> writes: > > >> How would I go about validating that in a rails way. >> > > My solution is like this: > > validates_inclusion_of :dob, :in => Date.new(1930).. > 18.years.ago.to_date, :message => "Must be born between 1930 and # > {18.years.ago.strftime(''%Y'')}" > > The 1930 thing is kind of arbitrary though, but this doesn''t require > extending AR. > -- > doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >