# How ca I validate two correlated fields?

**URL:** <https://www.ruby-forum.com/t/how-ca-i-validate-two-correlated-fields/120158>\
**Category:** Rails\
**Created:** [November 14, 2007, 8:31am UTC](https://www.ruby-forum.com/t/how-ca-i-validate-two-correlated-fields/120158 "2007-11-14T08:31:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jprogramista](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jprogramista](https://www.ruby-forum.com/u/jprogramista)\
**Post date:** [November 14, 2007, 8:31am UTC](https://www.ruby-forum.com/t/how-ca-i-validate-two-correlated-fields/120158/1 "2007-11-14T08:31:27Z")

</div>

Hello,  
I have problem with custom validator which should check such  
situation:  
I have two fields text\_field ‘object’, ‘others’ and text\_field  
‘object’, ‘others\_area’  
I “raise” error if others is nil but others\_area is filled up also  
when others is not nil but others\_area isn’t numeric  
If both fields are nil it’s ok i allow such situation.  
How can I do that in one correlated validator?

Best regards,  
Adr

---

<div class="post-metadata">

**Author:** ![jprogramista](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jprogramista](https://www.ruby-forum.com/u/jprogramista)\
**Post date:** [November 14, 2007, 8:36am UTC](https://www.ruby-forum.com/t/how-ca-i-validate-two-correlated-fields/120158/2 "2007-11-14T08:36:43Z")

</div>

Please attach some code or try explain better, because i don’t  
understand you

---

<div class="post-metadata">

**Author:** ![jprogramista](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jprogramista](https://www.ruby-forum.com/u/jprogramista)\
**Post date:** [November 14, 2007, 10:33am UTC](https://www.ruby-forum.com/t/how-ca-i-validate-two-correlated-fields/120158/3 "2007-11-14T10:33:49Z")

</div>

On Nov 14, 2:30 pm, jprogramista [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Best regards,  
> Adr

Simple. Use custom validation in your model of ‘object’.

Use something like this:

class Bla \< ActiveRecord::Base

def validate  
if others.nil? and others\_area.not\_numeric? then  
errors.add(“nasty thing”, “happenned”)  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![jprogramista](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jprogramista](https://www.ruby-forum.com/u/jprogramista)\
**Post date:** [November 14, 2007, 10:43am UTC](https://www.ruby-forum.com/t/how-ca-i-validate-two-correlated-fields/120158/4 "2007-11-14T10:43:59Z")

</div>

you need to write your own validation with validates\_each

something like:

validates\_each :others, :others\_area, :on =\> :create, do |record, attr,  
value|  
unless others then #check for nil  
… # do your validation here  
# and add error to record if necessary:  
record.errors.add :others, ‘your message’  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![jprogramista](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jprogramista](https://www.ruby-forum.com/u/jprogramista)\
**Post date:** [November 14, 2007, 11:28am UTC](https://www.ruby-forum.com/t/how-ca-i-validate-two-correlated-fields/120158/5 "2007-11-14T11:28:51Z")

</div>

On Nov 14, 10:43 am, Thorsten M. \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> you need to write your own validation with validates\_each
> 
> something like:
> 
> validates\_each :others, :others\_area, :on =\> :create, do |record, attr,

Hi thx for response, i tried to write way you described but stuck with  
such code:  
def validates\_correlated\_presence\_of(\*attr\_names)  
send(validation\_method(:create)) do |record|  
value1 = record.send(attr\_names[0])  
value2 = record.send(attr\_names[1])  
if(!value1.nil? || !value2.nil?)  
#validates\_presence\_of(attr\_names[0], attr\_names[1], :message =\>  
“must be filled”)  
validates\_presence\_of(attr\_names[0], :message =\> “must be  
filled”)  
validates\_presence\_of(attr\_names[1], :message =\> “must be  
filled”)  
end  
end  
end

Validator goes all lines of code but  
validates\_presence\_of(attr\_names[0], :message =\> “must be filled”)  
isn’t adding any errors to record object. Is it possible to embed one  
validator into another?

Best regards,  
Adr
