# At my wits end ! Controlling passed in negative values from a form

**URL:** <https://www.ruby-forum.com/t/at-my-wits-end-controlling-passed-in-negative-values-from-a-form/170967>\
**Category:** Rails\
**Created:** [June 24, 2009, 4:06pm UTC](https://www.ruby-forum.com/t/at-my-wits-end-controlling-passed-in-negative-values-from-a-form/170967 "2009-06-24T16:06:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rajive\_jain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@rajive\_jain](https://www.ruby-forum.com/u/rajive_jain)\
**Post date:** [June 24, 2009, 4:06pm UTC](https://www.ruby-forum.com/t/at-my-wits-end-controlling-passed-in-negative-values-from-a-form/170967/1 "2009-06-24T16:06:27Z")

</div>

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 ??

Thank you

---

<div class="post-metadata">

**Author:** ![rajive\_jain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@rajive\_jain](https://www.ruby-forum.com/u/rajive_jain)\
**Post date:** [June 24, 2009, 6:51pm UTC](https://www.ruby-forum.com/t/at-my-wits-end-controlling-passed-in-negative-values-from-a-form/170967/2 "2009-06-24T18:51:21Z")

</div>

2009/6/24 rajive jain [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> self.xxx = 0 unless self.xxx  
> 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

---

<div class="post-metadata">

**Author:** ![rajive\_jain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@rajive\_jain](https://www.ruby-forum.com/u/rajive_jain)\
**Post date:** [June 24, 2009, 8:50pm UTC](https://www.ruby-forum.com/t/at-my-wits-end-controlling-passed-in-negative-values-from-a-form/170967/3 "2009-06-24T20:50:33Z")

</div>

Dear Colin

Thank you for your insight… indeed you are right the conversion is  
happening before the validation - hence I thought before\_validation  
would catch it.

Anyways, I did move the checking to the controller and found something  
interesting.

In the controller, the rounding takes place at the time of the model’s  
create call e.g. @person = Person.new(params[:person]) or when saving.  
This I found by looking at the screen log and noting the attribute  
value in the params hash vs the db table’s insert call on the model.  
Hence one needs to check this at earlier stage in the controller when  
the raw params values come in via the post request.

Furthermore, at this stage, prior to @person = Person.new…  
definition, one has to be sure one has checked params is not nil,  
which is the simple if statement like:

if request.post? and params[:person]

So, yes it worked finally - thank you indeed so much for your  
insight ! 🙂

I am certain there has to be a better way to do this no … since it  
opens up an entire bunch of ‘check validation’ fears for me in the  
controller at the params level.

Thanks Colin and best regards
