Re: Validating DateHelpers date_select

Duane Johnson
Thu, 26 May 2005 14:07:09 -0000

> 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