Is it possible to use the built-in validation helper methods from within
my own validate() (or validate_save() or validate_create()) method, or
are they limited to only being used as class methods? I need to override
validate() but it would still be helpful to take advantage of the
existing helpers.
Thanks!
/afb
Adam B. wrote:
Is it possible to use the built-in validation helper methods from within
my own validate() (or validate_save() or validate_create()) method, or
are they limited to only being used as class methods? I need to override
validate() but it would still be helpful to take advantage of the
existing helpers.
Thanks!
/afb
validate is called in addition to any class_method defined validations,
not instead of. The following if perfectly valid:
class Foo < ActiveRecord::Base
validates_presence_of :bar
validates_numercality_of :some_integer
def validate
if !for_sale && price.nil?
errors.add :price, "foo's that are for sale must have a price!"
end
end
end
I’m trying to understand this by going through the source, but I don’t.
I know that these validation helpers are implemented as class methods,
presumably ‘extend’ is being called somewhere in ActiveRecord::Base in
order to effect that.
But I would then think that I should be able to say
include ActiveRecord::Validations::ClassMethods
in my class definition in order to get the validation helpers accessible
at the instance level. When I do that I get the following error when
validate is called:
SystemStackError: stack level too deep
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/validations.rb:403:in
validates_presence_of' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/validations.rb:402:in
each’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/validations.rb:402:in
validates_presence_of' from script/../config/../config/../app/models/web_user.rb:62:in
validate’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/validations.rb:403:in
send' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/validations.rb:403:in
validates_presence_of’
… 6446 levels…
Some sort of recursion is happening, but I’m at the end of my Ruby
abilities. Any help would be much appreciated. I’m trying to be DRY (or
at least not repeat others), but if I can’t figure this out soon I’ll
just have to rewrite the validations I need manually.
Thanks!
/afb
Thanks. Yeah, I’ve probably been thinking about this wrong. I need to do
a bunch of conditional validation. I know I can write a method to test
the conditions and pass that to the helpers in the :if parameter. It
just seemed cleaner to override validate(), but that would require a
bunch of extra code to do what I would otherwise do with the helpers. So
probably not in fact cleaner.
One thing I haven’t seen. I presume this is okay:
validates_presence_of :name, :address, :if :test1
validates_presence_of :name, :address, :phone, :if :test2
Is that the accepted way to implement that kind of conditional
validation?
/afb
Adam B. wrote:
Thanks. Yeah, I’ve probably been thinking about this wrong. I need to do
a bunch of conditional validation. I know I can write a method to test
the conditions and pass that to the helpers in the :if parameter. It
just seemed cleaner to override validate(), but that would require a
bunch of extra code to do what I would otherwise do with the helpers. So
probably not in fact cleaner.
One thing I haven’t seen. I presume this is okay:
validates_presence_of :name, :address, :if :test1
validates_presence_of :name, :address, :phone, :if :test2
Is that the accepted way to implement that kind of conditional
validation?
/afb
more like:
validates_presence_of :name, :address, :if => :foo?
def foo?
foo == ‘bar’
end
pretty sure that works. if it doesnt
validates_presence_of :name, :address, :if => Proc.new { foo? }