# Conditional validation

**URL:** <https://www.ruby-forum.com/t/conditional-validation/127277>\
**Category:** Rails\
**Created:** [January 27, 2008, 5:57pm UTC](https://www.ruby-forum.com/t/conditional-validation/127277 "2008-01-27T17:57:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 27, 2008, 5:57pm UTC](https://www.ruby-forum.com/t/conditional-validation/127277/1 "2008-01-27T17:57:48Z")

</div>

Hi,

How can I validate user info under certain conditions? For example, I  
just downloaded a plug-in that allows me to validate phone numbers,  
but what I would like is to only validate the phone number if it is  
not blank and if the user\_type\_id field is equal to one.

How can I achieve such a validation in my user model?

Thanks, - Dave

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 27, 2008, 7:17pm UTC](https://www.ruby-forum.com/t/conditional-validation/127277/2 "2008-01-27T19:17:15Z")

</div>

this is not tested, but I think you can do something like this:

validates\_format\_of :number, :with =\> /[0-9]/, :if =\> some\_method?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 28, 2008, 5:22pm UTC](https://www.ruby-forum.com/t/conditional-validation/127277/3 "2008-01-28T17:22:53Z")

</div>

Thanks. How do I pass arguments to “some\_method”? I am concerned if  
the :phone\_number attribute of the model is not empty and if  
the :user\_type\_id field is equal to 2.

- Dave

On Jan 27, 12:17 pm, Jimmy P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
