# How do I allow an empty value in my model?

**URL:** <https://www.ruby-forum.com/t/how-do-i-allow-an-empty-value-in-my-model/127851>\
**Category:** Rails\
**Created:** [February 1, 2008, 9:03pm UTC](https://www.ruby-forum.com/t/how-do-i-allow-an-empty-value-in-my-model/127851 "2008-02-01T21:03:31Z")\
**Posts on this page:** 4\
**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:** [February 1, 2008, 9:03pm UTC](https://www.ruby-forum.com/t/how-do-i-allow-an-empty-value-in-my-model/127851/1 "2008-02-01T21:03:31Z")

</div>

Hi,

If a user enters a value for the field “work\_phone”, I want to  
validate it against a reg-exp. However, if the field is completely  
blank, I’d like to allow that too. This doesn’t work, though

validates\_format\_of :work\_phone, :with =\> %r{^(+\d)_\s_((\d{3})  
\s\*)\*\d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}$}, :allow\_empty  
=\> true

What is the right way to validate the field in the model? - 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:** [February 1, 2008, 9:24pm UTC](https://www.ruby-forum.com/t/how-do-i-allow-an-empty-value-in-my-model/127851/2 "2008-02-01T21:24:37Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Hi,
> 
> If a user enters a value for the field “work\_phone”, I want to  
> validate it against a reg-exp. However, if the field is completely  
> blank, I’d like to allow that too. This doesn’t work, though
> 
> validates\_format\_of :work\_phone, :with =\> %r{^(+\d)_\s_((\d{3})  
> \s\*)\*\d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}$}, :allow\_empty  
> =\> true
> 
> What is the right way to validate the field in the model? - Dave

if you are on 2.0.2 then you can use :allow\_blank =\> true

---

<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:** [February 1, 2008, 9:54pm UTC](https://www.ruby-forum.com/t/how-do-i-allow-an-empty-value-in-my-model/127851/3 "2008-02-01T21:54:06Z")

</div>

I believe it’s 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:** [February 1, 2008, 10:05pm UTC](https://www.ruby-forum.com/t/how-do-i-allow-an-empty-value-in-my-model/127851/4 "2008-02-01T22:05:56Z")

</div>

allow\_blank is the answer. 5 stars. -

On Feb 1, 2:24 pm, “Vapor …” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
