SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    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"?

  2. #2
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  3. #3
    SitePoint Member
    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.

  4. #4
    SitePoint Wizard samsm's Avatar
    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?

  5. #5
    SitePoint Member
    Join Date
    Aug 2005
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by forward
    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"?
    I think you just need to add :message to the end like this

    validates_format_of :age_before_type_cast, :with => /^[\d]*$/, :message => "age is invalid"

  6. #6
    SitePoint Member
    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.

  7. #7
    SitePoint Evangelist lirux's Avatar
    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

  8. #8
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lirux
    validates_numericality_of :age, :message => "age must be a number"
    It is interesting to me that that works. I'd have expected it to fail for the same reason that validates_format_of fails. Why does that work?
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  9. #9
    SitePoint Member
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •