# Validates\_format\_of nil

**URL:** <https://www.ruby-forum.com/t/validates-format-of-nil/49004>\
**Category:** Rails\
**Created:** [November 28, 2005, 5:04pm UTC](https://www.ruby-forum.com/t/validates-format-of-nil/49004 "2005-11-28T17:04:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [November 28, 2005, 5:04pm UTC](https://www.ruby-forum.com/t/validates-format-of-nil/49004/1 "2005-11-28T17:04:30Z")

</div>

I’ve noticed that validates\_format\_of will return invalid if the field  
is nil.

Question 1) How should we declare a validation that will validate it  
with a regex if it is set, but, if the field is not set, just move on

Question 2) Why is it done this way? The other validators are only  
invoked if the field is set . If you want to declare the field  
mandatory, you add a validates\_presence\_of. This seems to me to be the  
best design - why does validates\_format\_of break this?

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [November 28, 2005, 7:13pm UTC](https://www.ruby-forum.com/t/validates-format-of-nil/49004/2 "2005-11-28T19:13:35Z")

</div>

Am I making a mistake here? If so, someone please clue me in.

Likewise, if my questions are unclear, please let me know, and I’ll try  
to clarify.

lstrecv wrote:

> I’ve noticed that validates\_format\_of will return invalid if the field  
> is nil.
> 
> Question 1) How should we declare a validation that will validate it  
> with a regex if it is set, but, if the field is not set, just move on
> 
> Question 2) Why is it done this way? The other validators are only  
> invoked if the field is set . If you want to declare the field  
> mandatory, you add a validates\_presence\_of. This seems to me to be the  
> best design - why does validates\_format\_of break this?

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [November 28, 2005, 11:31pm UTC](https://www.ruby-forum.com/t/validates-format-of-nil/49004/3 "2005-11-28T23:31:01Z")

</div>

— List R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > is nil.  
> > mandatory, you add a validates\_presence\_of. This  
> > seems to me to be the  
> > best design - why does validates\_format\_of break  
> > this?

I argued elsewhere that the only validation that  
should fail on nil is the validates\_presence\_of. That  
makes this concept orthogonal to all other  
validations. Results in less code most of the time to.

The argument was not accepted, so we’ve branched our  
rails tree, and it will have this behavior. I’ll be  
posting a big patch somewhere soon.

It was mentioned that this will be changed in a post  
1.0 release.

Shawn

* * *

Yahoo! DSL ? Something to write home about.  
Just $16.99/mo. or less.  
[dsl.yahoo.com](http://dsl.yahoo.com)

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [November 29, 2005, 2:55am UTC](https://www.ruby-forum.com/t/validates-format-of-nil/49004/4 "2005-11-29T02:55:36Z")

</div>

Couldn’t have said it better myself.

Note that some of the validations (ie, validates\_association) _do_  
follow this behavior.

Shawn, could you post a link to the discussion you mentioned, as well as  
to the announcement that this will be added?

Also, in the meanwhile, what is the preferred idiom for simulating this  
(ie, validates\_format\_of when not nil)?

shawn\_garbett wrote:

> — List R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > is nil.  
> > > mandatory, you add a validates\_presence\_of. This  
> > > seems to me to be the  
> > > best design - why does validates\_format\_of break  
> > > this?
> 
> I argued elsewhere that the only validation that  
> should fail on nil is the validates\_presence\_of. That  
> makes this concept orthogonal to all other  
> validations. Results in less code most of the time to.
> 
> The argument was not accepted, so we’ve branched our  
> rails tree, and it will have this behavior. I’ll be  
> posting a big patch somewhere soon.
> 
> It was mentioned that this will be changed in a post  
> 1.0 release.
> 
> Shawn
> 
> * * *
> 
> Yahoo! DSL ï¿½ Something to write home about.  
> Just $16.99/mo. or less.  
> [dsl.yahoo.com](http://dsl.yahoo.com)

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [November 29, 2005, 3:57pm UTC](https://www.ruby-forum.com/t/validates-format-of-nil/49004/5 "2005-11-29T15:57:15Z")

</div>

— List R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Couldn’t have said it better myself.
> 
> Note that some of the validations (ie,  
> validates\_association) _do_  
> follow this behavior.

Some will logically require nil be an error, but in  
general I think nil is an orthogonal concern.

> Shawn, could you post a link to the discussion you  
> mentioned, as well as  
> to the announcement that this will be added?
> 
> Also, in the meanwhile, what is the preferred idiom  
> for simulating this  
> (ie, validates\_format\_of when not nil)?

[http://dev.rubyonrails.org//ticket/928/](http://dev.rubyonrails.org//ticket/928/)

I’m going to start next week preparing the patch for  
submission.

Shawn G.

* * *

Yahoo! Mail - PC Magazine Editors’ Choice 2005

> **[\[object](https://mail.yahoo.com/)**
>
> \[object
