# Validates\_format\_of

**URL:** <https://www.ruby-forum.com/t/validates-format-of/92659>\
**Category:** Rails\
**Created:** [April 22, 2007, 6:10pm UTC](https://www.ruby-forum.com/t/validates-format-of/92659 "2007-04-22T18:10:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Afroz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@Afroz](https://www.ruby-forum.com/u/Afroz)\
**Post date:** [April 22, 2007, 6:10pm UTC](https://www.ruby-forum.com/t/validates-format-of/92659/1 "2007-04-22T18:10:22Z")

</div>

Hi everyone!  
I’m new to rails and have been playing around a bit. Unfortunately, I  
seem to be a bit stuck at the moment. Since I’m just playing around,  
I’m using a dynamic scaffold. In my model file (which is tied to a  
database table), I have the following line:  
validates\_format\_of :grade, :with =\> /^[0-9]+$/, :message =\> ‘should  
be zero or a positive integer’

‘grade’ is an int(6) column in my table. What I would expect is that  
if I enter some random letters such as “asdas” in a form, then the  
string should be considered invalid and should throw an error.  
However, this does not occur. Instead, the form saves the data into  
the table with a value of 0.  
During my tests, I changed [0-9] to [1-9]. On doing so, “asdas” would  
not be accepted anymore, but “123as” would. “123” would be saved to  
the database.  
Perhaps my understanding of the above regular expression is wrong? It  
is my first time using regexp. However, I wrote a little test program  
in ruby:  
tst = Array.new  
tst[0] = ‘letters’  
tst[1] = ‘123let’  
tst[2] = ‘let123’  
tst[3] = ‘1234’  
tst[4] = ‘5’  
tst[6] = ‘1.5’

tst.each do |type|  
puts “#{type}\n” if type =~ /^[0-9]+$/  
end

As I expect, only 1234 and 5 are printed. It seems to work as expected  
in this little program, but not during validation.  
Any help/suggestions would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![Afroz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@Afroz](https://www.ruby-forum.com/u/Afroz)\
**Post date:** [April 22, 2007, 10:08pm UTC](https://www.ruby-forum.com/t/validates-format-of/92659/2 "2007-04-22T22:08:41Z")

</div>

Yeah… I’ve seen something like this. I think rails is attempting to  
convert the input to int and just makes it zero if it gets letters. I  
know that sounds stupid, but I’m pretty sure that’s what it does.

The good news is that you should be able to use  
validates\_numericality\_of to check for letters. The problem with that is  
that as near as I can tell, you can’t specify a positive value (you can  
specify only\_integer though).

The trick that that validation uses – and an excellent thing to learn  
about rails – is it checks the value “before\_type\_cast”. ActiveRecord  
keeps a buffer of the input values before they are type cast and you can  
access them with \<field\_name\>\_before\_type\_cast.

So, you could try just changing :grade in your validates\_format\_of to  
:grade\_before\_type\_cast. Not sure if that will work at all, but it  
should in theory.

Hope this helps…

b
