Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

numericality validation gives misleading error message when only_integer is used

#4406

If one uses the only_integer option of the numericality validator the result is an error message that may look like the following:
"2.5 is not a number". That is a very misleading error message. Unless I am mistaken, 2.5 is very much a number. Most of the other options for the numericality validator result in customized error messages by default. So while I could pass in a custom error message every time I use ':only_integer => true', that seems less than ideal.

Reported by Jsmith45 · April 15th, 2010 @ 07:02 PM

State: resolved
Milestone: 3.0.2
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. Jason Langenauer
  2. Jsmith45
    Jsmith45
    • Title changed from numericality validation gives misleading error message only_integer is uses to numericality validation gives misleading error message when only_integer is used

    April 23rd, 2010 @ 05:59 AM

  3. Jason Langenauer
    Jason Langenauer
    • Tag set to patch

    Here's a patch with tests to resolve this issue - it's a patch against 2.3.5

    The only minor issue is that it hard-codes the use of "." as the numeric separator, which can be obviously be localized. The behavior here is consistent with the rest of ActiveRecord, however.

    April 23rd, 2010 @ 02:01 PM

  4. Rodrigo Navarro
    Rodrigo Navarro
    • Tag changed from patch to rails3 validations, patch

    Rails 3 ActiveModel has the same problem. I don't know if I should open another ticket or post the patch here. Anyways, here is the patch for rails 3.

    April 24th, 2010 @ 02:35 AM

  5. Rodrigo Navarro
    Rodrigo Navarro

    Fixing some typos, sorry, use this one instead.

    April 24th, 2010 @ 02:51 AM

  6. Jsmith45
    Jsmith45

    Jason, instead of testing for a decimal separator, why not just change the error message that occurs When the regex does not match? That regex is only run when only_integer is specified, and if said regex does not match then whatever was passed in is indisputably not an integer. That would also match Rodrigo's rails 3 patch.

    Otherwise I see no obvious issues with either patch, but I'm not an expert in rails internals.

    It probably does not need to be said, but if we don't open a second issue for Rails 3, let's make sure to land both patches at the same time, or to reopen after one lands so the second one closes the issue.

    April 24th, 2010 @ 06:09 AM

  7. Rodrigo Navarro
    Rodrigo Navarro

    Jsmith

    Exactly, at least the rails 3 version of the patch, it first tries to convert the string using Kernel.Float and only if that test pass we check if it is an integer (using the regex /\A[+-]?\d+\Z/).

    So Jason, as Jsmith said, try just to change the order of your checks, then you won't need to check for the ".", because the regex will do that for you.

    April 24th, 2010 @ 04:29 PM

  8. Scott Messinger
    Scott Messinger

    The patch doesn't apply to the master.

    Error:
    error: patch failed: activemodel/lib/active_model/validations/numericality.rb:44
    error: activemodel/lib/active_model/validations/numericality.rb: patch does not apply

    April 24th, 2010 @ 05:54 PM

  9. Rodrigo Navarro
    Rodrigo Navarro

    Sorry Scott, my bad. Probably some weird formating issues. This one applies successfully.

    April 24th, 2010 @ 06:22 PM

  10. Rodrigo Navarro
    Rodrigo Navarro

    This one is for 2.3.x. Checking for the integer with the regex and not with the hardcoded dot and with refactored tests.

    April 24th, 2010 @ 07:28 PM

  11. Scott Messinger
    Scott Messinger

    +1 This works for me for Rails 3 beta 3.

    April 24th, 2010 @ 08:02 PM

  12. Neeraj Singh
    Neeraj Singh

    Attached is a patch.

    I have no additional if options[:only_integer] condition. One place where all the validations happen returns the error code.

    April 24th, 2010 @ 08:16 PM

  13. Rodrigo Navarro
    Rodrigo Navarro

    Well that was almost like my first approach too, but I decided to keep the checks separated, I feel that it is easily to read and understand, but that is just my opinion.

    April 24th, 2010 @ 10:05 PM

  14. Neeraj Singh
    Neeraj Singh

    Hi Rodrigo,

    Sorry. I did not checkout all the patches. You are right it's pretty close.

    April 24th, 2010 @ 10:25 PM

  15. José Valim
    José Valim
    • Milestone cleared.
    • Assigned user set to José Valim

    April 24th, 2010 @ 10:29 PM

  16. Repository
  17. José Valim
    José Valim

    Excellent patch!

    I applied it just on the 3.0 branch. Not on the 2.3 branch because it includes some non-minor changes in the way the validator works.

    April 25th, 2010 @ 09:19 AM

  18. Neeraj Singh
    Neeraj Singh
    • Tag changed from rails3 validations, patch to rails3 validations, 2.3.6, patch

    Attached is a patch which is backport of the commit for 2-3-stable branch.

    April 25th, 2010 @ 05:49 PM

  19. Jsmith45
    Jsmith45

    Please not that the patch just committed for Rails 3 gives a "is not a number" error if the input is completely non-numeric such as an input of "xyzzy", and an error of "must be an integer" if it is numeric but non-integral.

    On the other hand I believe Neeraj's patch results in "must be an integer" in both cases. On the other hand Rodrigo's patch for 2.3 should give the same results as Rails 3.

    April 25th, 2010 @ 06:49 PM

  20. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to Low

    October 15th, 2010 @ 11:01 PM