This project is archived and is in readonly mode.
validates_length_of, :is does not work for numbers
-
Ryan Bigg
I can verify that this also doesn't work for me due to the fact it calls #size on a number.
-
Philip Hallstrom
Looks like you could fix this by patching lines 539 and 556 of activerecord/lib/active_record/validations.rb to convert 'value' to a String if it's any of the various number types. But that might break apps that expect validates_length_of on a number to test byte size (weird, but maybe they exist).
Just before the line that reads:
value = value.split(//) if value.kind_of?(String)
add:
value = value.to_s if value.kind_of?(Integer)
You'd want to catch Floats and BigNum and all the others too though.
-
Jose Fernandez
Created a patch for this issue. Modified the validates_length_of method in active_models/validations.rb so that all Numeric values get casted to a string before calling #size method on them. You can override this behavior (and go back to the old logic) by setting the new :byte_size option to true (false by default). Then the validation will check for the byte size of the Fixnum or Bignum value.
Created a set of tests that fully test this new feature too, all previous tests + the new ones pass.
-
Frederick Cheung
seems to me that it's pure luck that integers happen to have a size method (thank you duck typing).
Isn't this what the :greater_than etc... option of validates_numericality_of or validates_inclusion_of :foo, :in =>1000..10000 are for ?
-
Stephen Brown
I was attempting to use it for checking the length of an account number and sort code. I guess they could both be strings, but the bug is there regardless of my situation - a user would expect validates_length_of for a number to give size, not bytesize
-
Jose Fernandez
The first patch had reduntant Float check at line 549 that slipped in, updated the patch to remove it.
-
Philip Hallstrom
The problem with :greater_than or inclusion is that it doesn't work for zip codes (which I think is what the original post linked to as a problem).
If you're storing zip codes as integers, then you can't say :greater_than => 10000 an :less_than <= 99999 because 00501 is a valid zip code, but as an integer wouldn't fall in that range.
So in the zip code scenario you really do have to check the number of digits.
There are probably other situations as well...
-
Jose Fernandez
The old logic would also crash if you assign a float to the attribute being validated (manually of course because this would be impossible from within an html form), because it would call #size on the Float instance and crash.
-
Frederick Cheung
On 27 Aug 2008, at 16:27, Lighthouse wrote:
-
Frederick Cheung
D'oh screwed up the mail to reply thing. What I was saying is that if your data is something like 00500 then it shouldn't be a number, it should be a string. By the time it gets to a validation it will be typecast to 500 and so any sort of validates_length that checks it's 5 digits long will fail
-
Pratik
- Tag changed from 2.1, bug to 2.1, activerecord, bug, patch, validations
- Assigned user set to Frederick Cheung
- State changed from new to wontfix
Agree with what Fred said. You should be using strings.
