Validates_numericality_of, :allow_nil => true

I’m trying to use validates_numericality_of on a form of mine, using
:allow_nil => true, but when I submit the form with nothing filled in I
still get the “is not a number” error.

Help?

FYI: I’m using this right now…

validates_format_of :account_number, :with => /([0-9]*)/

…but it’s just annoying me that validates_numericality_of isn’t
working as
advertised.

Sorry for all the spam…this works better

/^[\d]*$/

subimage interactive <subimage@…> writes:

I’m trying to use validates_numericality_of on a form of mine, using
:allow_nil => true, but when I submit the form with nothing filled in I
still
get the “is not a number” error.Help?

When you don’t fill in a field on a form, the value that gets passed to
your
controller is NOT nil–it’s an empty string, “” (omitting the quotes, of
course). While it may seem pedantic, an empty string is still a string,
not a nil.

To test numericality and still allow the field to be empty you could do
something like this:
validates_numericality_of :some_field,
:if => Proc.new {|c| not
c.some_field.blank?}

–Forrest