SitePoint Sponsor |
|
User Tag List
Results 1 to 9 of 9
Hybrid View
-
Jan 18, 2006, 18:55 #1
- Join Date
- Dec 2005
- Posts
- 15
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Rails before_type_cast validation
In the database table, there is an integer column, lets call it age, and I ensure that only numbers (no + or - sign) are inputted with:
validates_format_of :age_before_type_cast, :with => /^[\d]*$/
This works fine but if an error message is generated, it would be something like "age before type cast is invalid". Is there a way to have the error message be more like "age is invalid"?
-
Jan 18, 2006, 19:05 #2
I thought the values were only typecast before the were saved, but after they are validated. I could be wrong though. If this is the case however, you shouldn't need to validate the before_type_cast field, just the normal age field.
-
Jan 18, 2006, 20:45 #3
- Join Date
- Dec 2005
- Posts
- 15
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks for the reply. It seems to typecast before validation. If I did this:
u = User.new
u.age = 'asdf'
u.save
with:
validates_format_of :age, :with => /^[\d]*$/
It'll save the age as zero (assuming from typecasting letters) in the database. I seem to be suffering from this unresolved ticket: "Models for tables with numeric columns quietly insert zeroes on invalid user input" (http://dev.rubyonrails.org/ticket/608)
I guess I'll check if its all digits in the validate method but I'm assuming there is a better way.
-
Jan 19, 2006, 22:57 #4
- Join Date
- Nov 2001
- Location
- Atlanta, GA, USA
- Posts
- 5,011
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
That is a killer gotcha.
My thanks to you for pointing that out, I'm not sure I would have been aware of that problem otherwise. It's also a reminder how helpful good, thorough testing can be, that's the only other way I'd see myself catching this.Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?
-
Jan 26, 2006, 06:31 #5
- Join Date
- Aug 2005
- Posts
- 2
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by forward
validates_format_of :age_before_type_cast, :with => /^[\d]*$/, :message => "age is invalid"
-
Jan 26, 2006, 19:19 #6
- Join Date
- Dec 2005
- Posts
- 15
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Unfortunately, that would produce "Age before type cast age is invalid". The message seems to only affect what comes after the name of the validated variable. Thanks for the tip though.
-
Jan 28, 2006, 16:58 #7
- Join Date
- Jan 2001
- Location
- Lisboa : Portugal
- Posts
- 418
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
validates_numericality_of :age, :message => "age must be a number"
Duarte Carrilho da Graça
RailsHelp.com: Searchable Rails reference
CACA: Committee for the Annihilation of Complicated Acronyms
-
Jan 28, 2006, 18:15 #8
- Join Date
- Nov 2001
- Location
- Atlanta, GA, USA
- Posts
- 5,011
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by lirux
Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?
-
Jan 28, 2006, 19:46 #9
- Join Date
- Dec 2005
- Posts
- 15
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks. That seems to work more like what one would expect. I'll do the other checks on the age input in the validate method.
Bookmarks