# Newbie validation

**URL:** <https://www.ruby-forum.com/t/newbie-validation/63821>\
**Category:** Rails\
**Created:** [June 19, 2006, 6:41pm UTC](https://www.ruby-forum.com/t/newbie-validation/63821 "2006-06-19T18:41:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![montyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/76d3ee/32.png) [@montyman](https://www.ruby-forum.com/u/montyman)\
**Post date:** [June 19, 2006, 6:41pm UTC](https://www.ruby-forum.com/t/newbie-validation/63821/1 "2006-06-19T18:41:48Z")

</div>

hi there. Here is the simple scenario:

My active record connects to a table that contains an integer field.

My form contains a text box that allows me to enter a value to get  
updated to the integer field.

If anything gets entered that is anything other than a POSITIVE INTEGER,  
I want to display an error.

I have noticed that if I create a new active record like  
@record = MyClass.new(params[:number])

and the number is a float, say 4.06, this will get rounded in the Active  
Record if i display it at this point  
render\_text @record.number …will show 4

Because it will get rounded automatically, I can’t tell that it is a  
non-integer number from within the active record and display an error.

Also, if after submitting the form, the active record will contain the  
rounded value (4) but on return to the form, the text field will contain  
the original value submitted, 4.06.

How can I intervene this behaviour, ie, test weather the value submitted  
is an INTEGER and return an error if it is not (including decimal  
values).

---

<div class="post-metadata">

**Author:** ![montyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/76d3ee/32.png) [@montyman](https://www.ruby-forum.com/u/montyman)\
**Post date:** [June 19, 2006, 9:20pm UTC](https://www.ruby-forum.com/t/newbie-validation/63821/2 "2006-06-19T21:20:35Z")

</div>

Hi, I would recommend adding the following to your controller:

validates\_numericality\_of :my\_positive\_integer, :gte =\> 0, :only\_integer  
=\>  
true

Also, I would recommending getting and reading “Agile Web D.  
with  
Rails”.

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![montyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/76d3ee/32.png) [@montyman](https://www.ruby-forum.com/u/montyman)\
**Post date:** [June 20, 2006, 5:48am UTC](https://www.ruby-forum.com/t/newbie-validation/63821/3 "2006-06-20T05:48:24Z")

</div>

Thanks for the solution, so simple.

I have actually read the agile book (most of it), but  
i’m wondering if there is a reference where I can self  
discover all the various switches to these methods,

for example the gte in validates\_numericality\_of. I couldn’t see  
this option in the online api reference.

Thanks for the help.

---

<div class="post-metadata">

**Author:** ![montyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/76d3ee/32.png) [@montyman](https://www.ruby-forum.com/u/montyman)\
**Post date:** [June 20, 2006, 10:19pm UTC](https://www.ruby-forum.com/t/newbie-validation/63821/4 "2006-06-20T22:19:37Z")

</div>

Hey Monty, if you would like to use the string “:gte =\> 0”, you’ll have  
to  
download and install the following plugin:

[http://www.umesd.com/better\_validates\_numericality\_of.zip](http://www.umesd.com/better_validates_numericality_of.zip)

Otherwise, the following should work for you:

validates\_numericality\_of :name, :only\_integer =\> true  
validates\_format\_of :name, :with =\> /^([0-9]+)$/, :on =\> :create,  
:on =\> :update

Finally, you would add that validation line to your model file instead  
of your controller.

Good luck,

-Conrad
