# Validates presence of special conditions not working

**URL:** <https://www.ruby-forum.com/t/validates-presence-of-special-conditions-not-working/181269>\
**Category:** Rails\
**Created:** [January 8, 2010, 3:59pm UTC](https://www.ruby-forum.com/t/validates-presence-of-special-conditions-not-working/181269 "2010-01-08T15:59:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [January 8, 2010, 3:59pm UTC](https://www.ruby-forum.com/t/validates-presence-of-special-conditions-not-working/181269/1 "2010-01-08T15:59:23Z")

</div>

Here is the validation being used which is not working…

validates\_presence\_of :name =\> :controller\_name, :if =\> :redirect?,  
:message =\> “must have the same name as the controller.”

The model has a string field for name.  
The model has a boolean field for redirect.  
The model has a string field for controller\_name.

What I want to do is ensure that if the redirect box is checked for  
true, that the name field matches the controller\_name field precisely.

I’m not sure if validates\_presence\_of will do it. I may have to create  
a custom validation but am unsure how to proceed.

Much appreciation in advance…

Thanks.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [January 8, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/validates-presence-of-special-conditions-not-working/181269/2 "2010-01-08T16:22:11Z")

</div>

On Jan 8, 2:59Â pm, Alpha B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What I want to do is ensure that if the redirect box is checked for  
> true, that the name field matches the controller\_name field precisely.
> 
> I’m not sure if validates\_presence\_of will do it. Â I may have to create  
> a custom validation but am unsure how to proceed.

validates\_presence\_of doesn’t do that.

Custom validations aren’t scary. Adding a class method like

def validates\_foo(\*attr\_names)  
options = attr\_names.extract\_options!  
validates\_each(attr\_names, options) do |record, attr\_name, value|  
record.errors.add(attr\_name, options[:message]) if …  
end  
end  
end

lets you do

validates\_foo :attr1, :attr2

You get stuff like :if and :unless for free

Fred

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [January 8, 2010, 6:30pm UTC](https://www.ruby-forum.com/t/validates-presence-of-special-conditions-not-working/181269/3 "2010-01-08T18:30:47Z")

</div>

Thanks for the explanation Fred.

I tried doing this but it’s not working properly:

def self.validates\_is\_exact(_attr\_names)  
options = attr\_names.extract\_options!  
validates\_each(_(attr\_names \<\< options)) do |record, attr\_name, value|  
if record.send( options[:compare\_field] ) == value  
record.errors.add(attr\_name, options[:message])  
end  
end  
true  
end

validates\_is\_exact :name, :compare\_field =\> :controller\_name, :if =\>  
:redirect?, :message =\> “must have the same name as the controller.”

And ran a test in the console:

e = Page.new  
e.name = “testname”  
e.redirect = true  
e.controller\_name = “testname”  
e.valid?

> > e.valid?  
> > =\> false

Any idea what I might be doing wrong here?

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [January 8, 2010, 6:36pm UTC](https://www.ruby-forum.com/t/validates-presence-of-special-conditions-not-working/181269/4 "2010-01-08T18:36:11Z")

</div>

I was reading the errors wrong. It’s working properly.

Thanks Fred for the advice.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [January 8, 2010, 6:47pm UTC](https://www.ruby-forum.com/t/validates-presence-of-special-conditions-not-working/181269/5 "2010-01-08T18:47:42Z")

</div>

Here is the finished validation, corrected:

def self.validates\_is\_exact(_attr\_names)  
options = attr\_names.extract\_options!  
validates\_each(_(attr\_names \<\< options)) do |record, attr\_name, value|  
if record.send( options[:compare\_field] ) != value  
record.errors.add(attr\_name, options[:message])  
end  
end  
true  
end

validates\_is\_exact :name, :compare\_field =\> :controller\_name, :if =\>  
:redirect?, :message =\> “must have the same name as the controller.”

Basically, it compares the :name field value with the :compare\_field  
field.value and if they are the same, validation is successful. If they  
are different, it throws my custom message.

Thanks again Fred. It was much easier than I anticipated.
