Validates_numericality_of, :allow_nil => true


#1

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?


#2

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.


#3

Sorry for all the spam…this works better

/^[\d]*$/


#4

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