Validates allow_nil not working as expected

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)

On Nov 28, 2007 3:50 PM, Greg W. [email protected] 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/

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

On Nov 28, 2007 3:50 PM, Greg W. [email protected] 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)

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

– gw

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