# Validates allow\_nil not working as expected

**URL:** <https://www.ruby-forum.com/t/validates-allow-nil-not-working-as-expected/121560>\
**Category:** Rails\
**Created:** [November 28, 2007, 10:51pm UTC](https://www.ruby-forum.com/t/validates-allow-nil-not-working-as-expected/121560 "2007-11-28T22:51:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 28, 2007, 10:51pm UTC](https://www.ruby-forum.com/t/validates-allow-nil-not-working-as-expected/121560/1 "2007-11-28T22:51:29Z")

</div>

I suspect this just doesn’t work like I’m expecting it too, but if I  
use :allow\_nil =\> TRUE, validation rules still complain if a form  
field is submitted empty (w/o using a presence\_of rule).

So…

```
validates_format_of :first_name, :with => Is_human_name,
   :allow_nil => TRUE, :message => Is_not_human_name_msg

```

Will complain if the field is submitted as empty. Now, empty doesn’t  
conform to the regex I am using, but I am expecting that :allow\_nil  
will cause the validation to be ignored when the value is empty.

(A) So what’s the point of allow\_nil? Or am I using it incorrectly?

Ultimately what I am trying to do is have certain messages override  
the need for others.

If I have this:

```
validates_presence_of :first_name

validates_format_of :first_name, :with => Is_human_name,
   :allow_nil => TRUE, :message => Is_not_human_name_msg

```

And the field is submitted as empty, then the only error message I  
need to see is the one that says the field cannot be empty. I do not  
need to see the format\_of error message.

(B) What’s the typical strategy for accomplishing that?

– gw (www.railsdev.ws)

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 28, 2007, 11:01pm UTC](https://www.ruby-forum.com/t/validates-allow-nil-not-working-as-expected/121560/2 "2007-11-28T23:01:28Z")

</div>

On Nov 28, 2007 3:50 PM, Greg W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> validates_format_of :first_name, :with => Is_human_name,
> :allow_nil => TRUE, :message => Is_not_human_name_msg
> 
> ```

I’d drop the :allow\_nil and use an :if =\> Proc.new { |model\_name|  
model\_name.first\_name.length \> 0 }

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 28, 2007, 11:58pm UTC](https://www.ruby-forum.com/t/validates-allow-nil-not-working-as-expected/121560/3 "2007-11-28T23:58:15Z")

</div>

On Nov 28, 2007, at 2:01 PM, Greg D. wrote:

> On Nov 28, 2007 3:50 PM, Greg W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ```
> > validates_format_of :first_name, :with => Is_human_name,
> > :allow_nil => TRUE, :message => Is_not_human_name_msg
> > 
> > ```
> 
> I’d drop the :allow\_nil and use an :if =\> Proc.new { |model\_name|  
> model\_name.first\_name.length \> 0 }

Bulky, but it works. Thanks.

– gw (www.railsdev.ws)

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 29, 2007, 1:18am UTC](https://www.ruby-forum.com/t/validates-allow-nil-not-working-as-expected/121560/4 "2007-11-29T01:18:56Z")

</div>

On Nov 28, 2007, at 1:50 PM, Greg W. wrote:

> conform to the regex I am using, but I am expecting that :allow\_nil  
> :allow\_nil =\> TRUE, :message =\> Is\_not\_human\_name\_msg
> 
> And the field is submitted as empty, then the only error message I  
> need to see is the one that says the field cannot be empty. I do not  
> need to see the format\_of error message.

Turns out :allow\_blank has been added for this very purpose…  
[http://dev.rubyonrails.org/ticket/7383](http://dev.rubyonrails.org/ticket/7383)

– gw

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 29, 2007, 12:49pm UTC](https://www.ruby-forum.com/t/validates-allow-nil-not-working-as-expected/121560/5 "2007-11-29T12:49:02Z")

</div>

true isn’t upper case … that may have been your problem - unless  
you’ve defined a constant TRUE?
