Defining Numericality 5

Posted by Bob Silva Fri, 27 Jan 2006 18:56:00 GMT

Now that I've been promoted to a management position, I don't get the opportunity to program as much as I'd like. I love sharing my knowledge of something I am passionate about and feel obligated to repay the debt I accumlated when I was learning how to program 10 years ago. So I monitor the Ruby on Rails mailing list for people in distress and throw on my cape and try to come to their rescue.

I had just such an opportunity last week, when this distressed soul wanted to validate that an input field was a positive integer.

This should be an easy one to figure out. validates_numericality_of sounds like the perfect remedy. As it turns out, validates_numericality_of only determines if its input is indeed a number. The only constraint you can specify is that its an integer as opposed to a decimal value.

My other choice would be validates_inclusion_of. Using the :in parameter to that method allows me to constrain the input value. I can specify that I want it to be within 0 and 4294967295.


validates_inclusion_of :int_field, :in => 0..4294967295

Not very pretty or intuitive. I got to wondering why this functionality couldn't be included in the validates_numericality_of routine. 'Numericality' is made up of 2 parts. 'Numerical' and 'ity'. We all know what 'Numerical' means (of or relating to numbers) and 'ity' is used to convert an adjective to a noun. Basically, it means we have a number. Therefore, 'Numericality' doesn't inherently constrain the value of a number, and in my opinion, neither should validates_numericality_of.


:validates_numericality_of :my_positive_integer, :gte => 0

Problem solved. You can download this plugin, which extends validates_numericality_of with 7 new options:

  • :gt - Greater than
  • :gte - Greater than or equal
  • :eq - Equal to
  • :lt - Less than
  • :lte - Less than or equal
  • :even - Even number
  • :odd - Odd number

Another lost soul rescued and now I must hang up my cape, return to my regular job and wait for another opportunity to jump into action.

UPDATE: This has been submitted as a patch against trunk.
http://dev.rubyonrails.org/ticket/3952

Comments

Leave a response

  1. Great! 6 months later:
    Thanks for this simple plugin, it's really cool!
  2. Awesome! 6 months later:
    Thanks! I came across the RoR mailing list post about this first before stumbling upon this article from a slightly different Google search.
  3. Problems 7 months later:
    I am running into trouble with the 'gte' and 'lte' operators somehow they fail to work. The error message doesn't seem to be added to the error messages for those operators. I am not sure what exactly is the problem. The other operators work just fine.
  4. yves_dufour@mac.com 9 months later:
    Why it's not possible to write ? :validates_numericality_of :my_integer1, :gte = 0 :validates_numericality_of :my_integer2, :gte = :my_integer1 would be a must.... yd
  5. PMc about 1 year later:
    There is some strangeness here:
    1.
    A not-equal operator is missing. I do not see the real sense in an equal operator - if there should be a predefined specific value for a field, then why open a field at all? But I see lot of sense in a non-equal operator, especially in each shopping system you do not want an item-count 0 entered.
    2.
    There is no longer checked against general numericality as soon as there is a comparison added. I do not see why an erroneous entry like maybe "19%0" should still be compared but not be detected as erroneous.
    3.
    If I request ":gte => 2.5" and enter 2.5, I still get an error. This is obvious from the code, as there is a conversion to_i done. I do not see why.
    4.
    As soon as one of the comparisons is used, the error message given by :message is no longer used, instead a static one from the plugin is used.
    5.
    I do not understand the code.
Comments