2009/6/24 rajive jain <[email protected]>: > > Hi, > > 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 > like so: > 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 ??
I am guessing a bit here but I assume what is happening here is that the -.003 is being rounded to 0 because you have specified decimal 9,2. Then the validation is run, but as the value is now zero, which is not negative, then the validation passes. Using before_validation does not help as this is probably after the rounding. I have little doubt that there is a better way but it you could do this check in the controller before it is rounded. Colin --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---

