I am trying to validate a numeric value passed in from a form, which
is saved in a MySQL db as decimal(9,2)
Now problem is I need to ensure people don't enter a form value like -.
003. In my validations, I have a validates_numericality_of check being
done. Furthermore, in validate method, I have a check using an if
statement such as :
((an_object[n].nil? or an_object[n] < 0) and !an_object[n].blank?)
While testing, Rails validation can catch -0.003. However, it just
does not catch -.003 (without leading zero). In fact, in case of -.
003, the value gets saved as 0; if form value is -.008 let's say, it
gets saved as .01 in db (gets rounded off). Anything like -1.23 works
ok - meaning it is trapped as error.
I have a before_validation method which assigns 0 to undefined values
self.xxx = 0 unless self.xxx
(where xxx is name of attribute)
I have tried checking for a minus sign in before_validation & validate
method using xxx.to_s.include?("-"). I have also tried
using :greater_than and :minimum options in the
validates_numericality_of call. I also tried validates_inclusion_of,
giving a range from 0..9999999.99.
Nothing seems to work.
Would appreciate any insight into this ! ? What am I missing ??