Forum: Ruby on Rails validates_numericality_of, :allow_nil => true

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Efe398b3b5436b0a8d4430090e8f86ea?d=identicon&s=25 subimage (Guest)
on 2005-12-04 02:18
(Received via mailing list)
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?
Efe398b3b5436b0a8d4430090e8f86ea?d=identicon&s=25 subimage (Guest)
on 2005-12-04 02:42
(Received via mailing list)
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.
Efe398b3b5436b0a8d4430090e8f86ea?d=identicon&s=25 subimage (Guest)
on 2005-12-04 03:39
(Received via mailing list)
Sorry for all the spam...this works better

/^[\d]*$/
2ee1028723473faa763884e902ce3366?d=identicon&s=25 thiessen (Guest)
on 2005-12-04 13:39
(Received via mailing list)
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
This topic is locked and can not be replied to.