# I only want to validate when field is not empty

**URL:** <https://www.ruby-forum.com/t/i-only-want-to-validate-when-field-is-not-empty/127470>\
**Category:** Rails\
**Created:** [January 29, 2008, 4:53pm UTC](https://www.ruby-forum.com/t/i-only-want-to-validate-when-field-is-not-empty/127470 "2008-01-29T16:53:12Z")\
**Posts on this page:** 6\
**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 29, 2008, 4:53pm UTC](https://www.ruby-forum.com/t/i-only-want-to-validate-when-field-is-not-empty/127470/1 "2008-01-29T16:53:12Z")

</div>

Hi,

I have a field in my user model:

Work phone  
\<%= text\_field 'user', 'work\_phone' %\>

But I only want to validate it if it is non-empty. So I put this into  
my user model:

validates\_as\_phone :work\_phone, :if =\> !:work\_phone.blank?  
&& :is\_client?, :message =\> “is not a valid phone number”

But if I leave the field blank and submit the form, the form is  
returned to me with the message “Work phone is not a valid phone  
number:”. What is wrong with the above statement?

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 29, 2008, 5:09pm UTC](https://www.ruby-forum.com/t/i-only-want-to-validate-when-field-is-not-empty/127470/2 "2008-01-29T17:09:51Z")

</div>

On Jan 29, 2008, at 9:51 AM, removed\_email\_address@domain.invalid wrote:

> validates\_as\_phone :work\_phone, :if =\> !:work\_phone.blank?  
> && :is\_client?, :message =\> “is not a valid phone number”
> 
> But if I leave the field blank and submit the form, the form is  
> returned to me with the message “Work phone is not a valid phone  
> number:”. What is wrong with the above statement?
> 
> Thanks, - Dave

Try :allow\_nil =\> true

Peace,  
Phillip

---

<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 29, 2008, 6:00pm UTC](https://www.ruby-forum.com/t/i-only-want-to-validate-when-field-is-not-empty/127470/3 "2008-01-29T18:00:48Z")

</div>

Thanks. It’s still not working but I’m using a “validate\_as\_phone”  
plugin

validates\_as\_phone :work\_phone, :allow\_nil =\> true, :message  
=\> “is not a valid phone number”

How did you envision doing this without such a plug-in? Best, - 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 29, 2008, 6:10pm UTC](https://www.ruby-forum.com/t/i-only-want-to-validate-when-field-is-not-empty/127470/4 "2008-01-29T18:10:53Z")

</div>

On Jan 29, 2008, at 10:59 AM, removed\_email\_address@domain.invalid wrote:

> Thanks. It’s still not working but I’m using a “validate\_as\_phone”  
> plugin
> 
> validates\_as\_phone :work\_phone, :allow\_nil =\> true, :message  
> =\> “is not a valid phone number”
> 
> How did you envision doing this without such a plug-in? Best, - Dave

Didn’t see that as a plugin. Sorry. It seems that the plugin would  
need to support blank values.

Apart from using the plugin, I guess you could use some regex. Look  
in the plugin to see how it’s done. You could probably just create  
your own private method in the class to handle it. Or, you might be  
able to extend the plugin.

Peace,  
Phillip

---

<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 30, 2008, 12:46am UTC](https://www.ruby-forum.com/t/i-only-want-to-validate-when-field-is-not-empty/127470/5 "2008-01-30T00:46:57Z")

</div>

Thanks for this feedback. I am very open to using other methods of  
validating US phone numbers, I just happened to find this one in a  
Google search. What do you use? - 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 29, 2008, 10:16pm UTC](https://www.ruby-forum.com/t/i-only-want-to-validate-when-field-is-not-empty/127470/6 "2008-01-29T22:16:10Z")

</div>

Not used the validates\_as\_phone plugin before, but looking at the  
source for validates\_as\_phone, it is only allowing lengths of 7, 10  
and 11 for the string length.  
You could either ‘monkey patch’ the plugin to do what you want (e.g.  
accept string length of 0), or use the plugin’s code as a base for  
writing your own validation plugin.

Rails 2 also has an allow\_blank property for validations but i’m not  
sure if this would be supported by this custom plugin. I can’t see  
anywhere in the code where it would check this explicitly but you  
could try this if you are on v2. allow\_nil will only validate if the  
actual value from the parameters hash is nil, not if it is “”

The plugin doesn’t really look too amazing, to be honest, It only  
strips out any non-numeric characters and checks for length. Sure that  
US phone numbers are more complex than that! It is also not very  
optimised, calling the same methods several times (namely  
new\_value.length) instead of storing this computation in a variable.

Good luck  
joe
