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?
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs