# Validation helpers in my custom validate method?

**URL:** <https://www.ruby-forum.com/t/validation-helpers-in-my-custom-validate-method/74332>\
**Category:** Rails\
**Created:** [November 4, 2006, 6:31pm UTC](https://www.ruby-forum.com/t/validation-helpers-in-my-custom-validate-method/74332 "2006-11-04T18:31:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [November 4, 2006, 6:31pm UTC](https://www.ruby-forum.com/t/validation-helpers-in-my-custom-validate-method/74332/1 "2006-11-04T18:31:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [November 6, 2006, 6:25am UTC](https://www.ruby-forum.com/t/validation-helpers-in-my-custom-validate-method/74332/2 "2006-11-06T06:25:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [November 6, 2006, 5:21am UTC](https://www.ruby-forum.com/t/validation-helpers-in-my-custom-validate-method/74332/3 "2006-11-06T05:21:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [November 6, 2006, 6:55am UTC](https://www.ruby-forum.com/t/validation-helpers-in-my-custom-validate-method/74332/4 "2006-11-06T06:55:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [November 6, 2006, 8:54am UTC](https://www.ruby-forum.com/t/validation-helpers-in-my-custom-validate-method/74332/5 "2006-11-06T08:54:33Z")

</div>

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? }
