# Validation with :if params

**URL:** <https://www.ruby-forum.com/t/validation-with-if-params/118032>\
**Category:** Rails\
**Created:** [October 23, 2007, 2:10pm UTC](https://www.ruby-forum.com/t/validation-with-if-params/118032 "2007-10-23T14:10:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mindmage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f4b2a3/32.png) [@mindmage](https://www.ruby-forum.com/u/mindmage)\
**Post date:** [October 23, 2007, 2:10pm UTC](https://www.ruby-forum.com/t/validation-with-if-params/118032/1 "2007-10-23T14:10:06Z")

</div>

Hi all!  
I’m trying to make a model validation that starts only if a given  
attribute has a specified value, but it seems i’m doing something wrong  
🙂

I have 3 values: respond\_via, email, telephone\_number. I want the latter  
ones to validate only if respond\_via has specific value.

So far i have:  
validates\_presence\_of :email, :if =\> (:respond\_via == “e-mail”)  
validates\_presence\_of :telephone\_number, :if =\> (:respond\_via ==  
“telephone”)

, but this version raises undefined method `respond\_via’ exception.

I am sure it is defined, so it must be my syntax that is to blame. Any  
suggestions?

---

<div class="post-metadata">

**Author:** ![mindmage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f4b2a3/32.png) [@mindmage](https://www.ruby-forum.com/u/mindmage)\
**Post date:** [October 23, 2007, 2:30pm UTC](https://www.ruby-forum.com/t/validation-with-if-params/118032/2 "2007-10-23T14:30:59Z")

</div>

On 10/23/07, Mind M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> validates\_presence\_of :email, :if =\> (:respond\_via == “e-mail”)  
> validates\_presence\_of :email :if =\> Proc.new {|model|  
> model.respond\_via == “e\_mail”}

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![mindmage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f4b2a3/32.png) [@mindmage](https://www.ruby-forum.com/u/mindmage)\
**Post date:** [October 23, 2007, 2:44pm UTC](https://www.ruby-forum.com/t/validation-with-if-params/118032/3 "2007-10-23T14:44:07Z")

</div>

Rick Denatale wrote:

> validates\_presence\_of :email :if =\> Proc.new {|model|  
> model.respond\_via == “e\_mail”}

Thanks a lot, this worked 🙂
