This project is archived and is in readonly mode.
numericality validation gives misleading error message when only_integer is used
-
Jason Langenauer
Verified in Rails 2.3.5
-
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
-
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.
-
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.
-
Rodrigo Navarro
Fixing some typos, sorry, use this one instead.
-
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.
-
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.
-
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 -
Rodrigo Navarro
Sorry Scott, my bad. Probably some weird formating issues. This one applies successfully.
-
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.
-
Scott Messinger
+1 This works for me for Rails 3 beta 3.
-
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.
-
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.
-
Neeraj Singh
Hi Rodrigo,
Sorry. I did not checkout all the patches. You are right it's pretty close.
-
José Valim
- Milestone cleared.
- Assigned user set to José Valim
-
Repository
- State changed from new to resolved
(from [77c099c231f2efb36a2a77a32138ed5c6761ec19]) Fix validates_numericaly_of only integer error message [#4406 numericality validation gives misleading error message when only_integer is used state:resolved]
Signed-off-by: José Valim jose.valim@gmail.com
http://github.com/rails/rails/commit/77c099c231f2efb36a2a77a32138ed... -
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.
-
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.
-
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.
