# Other validations fail even if there is no validates\_presence\_of

**URL:** <https://www.ruby-forum.com/t/other-validations-fail-even-if-there-is-no-validates-presence-of/120760>\
**Category:** Rails\
**Created:** [November 20, 2007, 12:51pm UTC](https://www.ruby-forum.com/t/other-validations-fail-even-if-there-is-no-validates-presence-of/120760 "2007-11-20T12:51:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Love\_AJAX](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/779978/32.png) [@Love\_AJAX](https://www.ruby-forum.com/u/Love_AJAX)\
**Post date:** [November 20, 2007, 12:51pm UTC](https://www.ruby-forum.com/t/other-validations-fail-even-if-there-is-no-validates-presence-of/120760/1 "2007-11-20T12:51:20Z")

</div>

Hi,

I have a set of validations like:  
validates\_format\_of :city,  
:with =\> /^[a-zA-Z\s’&]+$/,  
:message =\> “is not a valid name”

validates\_format\_of :zip,  
:with =\> /^[\d]{5}+$/,  
:allow\_nil =\> true,  
:message =\> “is not a valid zip code”

But I don’t have a validates\_presence\_of :city, :zip.

I want to validate the format only if user has entered some value into  
the fields. But both the \_format\_of hooks are trying to validate an  
empty value. Any idea how I can stop this behavior?

Thanks.

---

<div class="post-metadata">

**Author:** ![Love\_AJAX](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/779978/32.png) [@Love\_AJAX](https://www.ruby-forum.com/u/Love_AJAX)\
**Post date:** [November 20, 2007, 4:54pm UTC](https://www.ruby-forum.com/t/other-validations-fail-even-if-there-is-no-validates-presence-of/120760/2 "2007-11-20T16:54:26Z")

</div>

Add :allow\_nil =\> true to the city validation. You only have it on the  
zip code validation.

-Bill

Love AJAX wrote:

> ```
> :message => "is not a valid zip code"
> 
> ```

–  
Sincerely,

William P.

---

<div class="post-metadata">

**Author:** ![Love\_AJAX](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/779978/32.png) [@Love\_AJAX](https://www.ruby-forum.com/u/Love_AJAX)\
**Post date:** [November 21, 2007, 7:13am UTC](https://www.ruby-forum.com/t/other-validations-fail-even-if-there-is-no-validates-presence-of/120760/3 "2007-11-21T07:13:30Z")

</div>

:allow\_nil =\> true didn’t work on these validations. Instead I had to  
do something like :if =\> Proc.new{|profile| profile != “”}. This works  
fine. So my code now looks like this:

validates\_format\_of :zip,  
:with =\> /^[\d]{5}+$/,  
:if =\> Proc.new {|profile| profile.zip != “”},  
:message =\> “is not a valid zip code”

validates\_format\_of :city,  
:with =\> /^[a-zA-Z\s'&]+$/,  
:if =\> Proc.new {|profile| profile.city != “”},  
:message =\> “is not a valid name”

See the last sentence of  
[Peak Obsession](http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html#M000958).

Thanks much.
