# Custom Validations

**URL:** <https://www.ruby-forum.com/t/custom-validations/93404>\
**Category:** Rails\
**Created:** [April 1, 2006, 10:36pm UTC](https://www.ruby-forum.com/t/custom-validations/93404 "2006-04-01T22:36:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robert\_MannI](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ac91a4/32.png) [@Robert\_MannI](https://www.ruby-forum.com/u/Robert_MannI)\
**Post date:** [April 1, 2006, 10:36pm UTC](https://www.ruby-forum.com/t/custom-validations/93404/1 "2006-04-01T22:36:25Z")

</div>

Does anyone how to create a validation that:

validates\_presence\_of :a OR :b

I.e. :a OR :b must be present.

I thought this would probably be possible with some kind of validation  
callback, e.g.:

def check\_a\_or\_b\_is\_set  
return (a or b)  
end

Any ideas?

Thanks,  
Rob

---

<div class="post-metadata">

**Author:** ![Robert\_MannI](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ac91a4/32.png) [@Robert\_MannI](https://www.ruby-forum.com/u/Robert_MannI)\
**Post date:** [April 1, 2006, 11:22pm UTC](https://www.ruby-forum.com/t/custom-validations/93404/2 "2006-04-01T23:22:36Z")

</div>

How about something like:

validates\_each :a do |record, attribute, value|  
if value.blank? and record.send(:b).blank?  
record.errors.add\_to\_base ‘a or b required’  
end  
end

---

<div class="post-metadata">

**Author:** ![Robert\_MannI](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ac91a4/32.png) [@Robert\_MannI](https://www.ruby-forum.com/u/Robert_MannI)\
**Post date:** [April 2, 2006, 4:06am UTC](https://www.ruby-forum.com/t/custom-validations/93404/3 "2006-04-02T04:06:48Z")

</div>

Another way to do ‘custom’ validation is to simply supply a “validate”  
method in your class. This gets called before you save/update. You can  
simplify a bit what Ian has suggested.

#just place this in your model class  
def validate  
errors.add\_to\_base “A or B are required” if a.blank? and b.blank?  
end

Hope this helps!  
-Nick

---

<div class="post-metadata">

**Author:** ![Robert\_MannI](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ac91a4/32.png) [@Robert\_MannI](https://www.ruby-forum.com/u/Robert_MannI)\
**Post date:** [April 2, 2006, 4:12am UTC](https://www.ruby-forum.com/t/custom-validations/93404/4 "2006-04-02T04:12:50Z")

</div>

You could do something like:

validates\_presence\_of :a, :if =\> Proc.new { |r| !r.b? }  
validates\_presence\_of :b, :if =\> Proc.new { |r| !r.a? }

-Jonny.

---

<div class="post-metadata">

**Author:** ![Robert\_MannI](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ac91a4/32.png) [@Robert\_MannI](https://www.ruby-forum.com/u/Robert_MannI)\
**Post date:** [April 2, 2006, 10:50am UTC](https://www.ruby-forum.com/t/custom-validations/93404/5 "2006-04-02T10:50:45Z")

</div>

Thanks alot for all three suggestions!

The “validate” callback was what I was looking for, as it allows me to  
make  
the validations as complex as I like.

Thanks!

Rob
