# I only want to validate when field is not empty

**URL:** <https://discuss.rubyonrails.org/t/i-only-want-to-validate-when-field-is-not-empty/24023>\
**Category:** rubyonrails-talk\
**Created:** [January 29, 2008, 3:51pm UTC](https://discuss.rubyonrails.org/t/i-only-want-to-validate-when-field-is-not-empty/24023 "2008-01-29T15:51:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![laredotornado1](https://avatars.discourse-cdn.com/v4/letter/l/3da27b/32.png) [@laredotornado1](https://discuss.rubyonrails.org/u/laredotornado1)\
**Post date:** [January 29, 2008, 3:51pm UTC](https://discuss.rubyonrails.org/t/i-only-want-to-validate-when-field-is-not-empty/24023/1 "2008-01-29T15:51:59Z")

</div>

Hi,

I have a field in my user model:

\<p\>\<label for="user\_work\_phone"\>Work phone\</label\>\<br/\> \<%= text\_field 'user', 'work\_phone' %\>\</p\>

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

&nbsp;&nbsp;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:** ![Phillip\_Koebbe](https://avatars.discourse-cdn.com/v4/letter/p/d9b06d/32.png) [@Phillip\_Koebbe](https://discuss.rubyonrails.org/u/Phillip_Koebbe)\
**Post date:** [January 29, 2008, 4:08pm UTC](https://discuss.rubyonrails.org/t/i-only-want-to-validate-when-field-is-not-empty/24023/2 "2008-01-29T16:08:42Z")

</div>

Try :allow\_nil =\> true

Peace, Phillip

---

<div class="post-metadata">

**Author:** ![laredotornado1](https://avatars.discourse-cdn.com/v4/letter/l/3da27b/32.png) [@laredotornado1](https://discuss.rubyonrails.org/u/laredotornado1)\
**Post date:** [January 29, 2008, 4:59pm UTC](https://discuss.rubyonrails.org/t/i-only-want-to-validate-when-field-is-not-empty/24023/3 "2008-01-29T16:59:23Z")

</div>

Thanks. It's still not working but I'm using a "validate\_as\_phone" plugin

&nbsp;&nbsp;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:** ![Phillip\_Koebbe](https://avatars.discourse-cdn.com/v4/letter/p/d9b06d/32.png) [@Phillip\_Koebbe](https://discuss.rubyonrails.org/u/Phillip_Koebbe)\
**Post date:** [January 29, 2008, 5:09pm UTC](https://discuss.rubyonrails.org/t/i-only-want-to-validate-when-field-is-not-empty/24023/4 "2008-01-29T17:09:49Z")

</div>

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:** ![Wildtangent](https://avatars.discourse-cdn.com/v4/letter/w/df788c/32.png) [@Wildtangent](https://discuss.rubyonrails.org/u/Wildtangent)\
**Post date:** [January 29, 2008, 9:15pm UTC](https://discuss.rubyonrails.org/t/i-only-want-to-validate-when-field-is-not-empty/24023/5 "2008-01-29T21:15:08Z")

</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

---

<div class="post-metadata">

**Author:** ![laredotornado1](https://avatars.discourse-cdn.com/v4/letter/l/3da27b/32.png) [@laredotornado1](https://discuss.rubyonrails.org/u/laredotornado1)\
**Post date:** [January 29, 2008, 11:46pm UTC](https://discuss.rubyonrails.org/t/i-only-want-to-validate-when-field-is-not-empty/24023/6 "2008-01-29T23:46:12Z")

</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
