# Validates\_format\_of / nil

**URL:** <https://www.ruby-forum.com/t/validates-format-of-nil/115494>\
**Category:** Rails\
**Created:** [September 25, 2007, 7:48pm UTC](https://www.ruby-forum.com/t/validates-format-of-nil/115494 "2007-09-25T19:48:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![masonkessinger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4da419/32.png) [@masonkessinger](https://www.ruby-forum.com/u/masonkessinger)\
**Post date:** [September 25, 2007, 7:48pm UTC](https://www.ruby-forum.com/t/validates-format-of-nil/115494/1 "2007-09-25T19:48:43Z")

</div>

When using validates\_format\_of I am not able to enter a nil value?

It’s a mute point that I think that validates\_format\_of should NOT do  
the job of validates\_presence\_of, but how should i get around this?

I want to validate a field only if someone has entered something in,  
otherwise i do not want it to validate nothing. I do not mind if he user  
enters nothing, but if they intend for something to be there, it needs  
the correct format.

HELP!?

thanks!  
mk

---

<div class="post-metadata">

**Author:** ![masonkessinger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4da419/32.png) [@masonkessinger](https://www.ruby-forum.com/u/masonkessinger)\
**Post date:** [September 25, 2007, 8:17pm UTC](https://www.ruby-forum.com/t/validates-format-of-nil/115494/2 "2007-09-25T20:17:30Z")

</div>

Validations provide mechanisms to do what you’re asking. See the API on  
validations for more info.

Possible solutions can be found via google “validates\_format\_of nil”

> **[Apply rails validation rules only when the attribute is not nil](https://significantbits.wordpress.com/2007/03/02/apply-rails-validation-rules-only-when-the-attribute-is-not-nil/)**
>
> If you want to a validation rule to be applied only when the corresponding attribute has a value, you can use the :allow\_nil =\> true parameter. I don’t know if this works on any Rails mode…

[http://brighter.net/2007/6/23/validates\_format\_of-something-without-validates\_presence\_of](http://brighter.net/2007/6/23/validates_format_of-something-without-validates_presence_of)

Good luck!

---

<div class="post-metadata">

**Author:** ![masonkessinger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4da419/32.png) [@masonkessinger](https://www.ruby-forum.com/u/masonkessinger)\
**Post date:** [September 25, 2007, 8:44pm UTC](https://www.ruby-forum.com/t/validates-format-of-nil/115494/3 "2007-09-25T20:44:35Z")

</div>

validates\_format\_of :foo, :with =\> /bar/, :if =\> Proc.new(|o| o.foo !=  
nil }

Mason K. wrote:

> HELP!?
> 
> thanks!  
> mk

–  
Sincerely,

William P.

---

<div class="post-metadata">

**Author:** ![masonkessinger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4da419/32.png) [@masonkessinger](https://www.ruby-forum.com/u/masonkessinger)\
**Post date:** [September 25, 2007, 8:59pm UTC](https://www.ruby-forum.com/t/validates-format-of-nil/115494/4 "2007-09-25T20:59:37Z")

</div>

:allow\_nil works for most of the validates\_ calls, but it is not an  
option of validates\_format\_of

Mason K. wrote:

> Thanks!

–  
Sincerely,

William P.

---

<div class="post-metadata">

**Author:** ![masonkessinger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4da419/32.png) [@masonkessinger](https://www.ruby-forum.com/u/masonkessinger)\
**Post date:** [September 25, 2007, 8:53pm UTC](https://www.ruby-forum.com/t/validates-format-of-nil/115494/5 "2007-09-25T20:53:43Z")

</div>

Thank you!

:allow\_nil =\> true

did not work (though some said it worked fine for them)

:if =\> column\_name?

did work though.

Thanks!
