Skip to content
This repository
Browse code

r3604@asus: jeremy | 2005-09-26 19:10:42 -0700

 Treat validates_size_of :within as a single validator rather than splitting it into to ends of the range, thus yield two error messages on nil (yuck)


git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2355 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 6d7551f1bb9d2cc66d6db12bfccb5585f28118c3 1 parent 54adccd
Jeremy Kemper authored
49  activerecord/lib/active_record/validations.rb
@@ -21,8 +21,8 @@ def initialize(base) # :nodoc:
21 21
       :too_short => "is too short (min is %d characters)", 
22 22
       :wrong_length => "is the wrong length (should be %d characters)", 
23 23
       :taken => "has already been taken",
24  
-      :not_a_number => "is not a number",
25  
-      }
  24
+      :not_a_number => "is not a number"
  25
+    }
26 26
       
27 27
     # Holds a hash with all the default error messages, such that they can be replaced by your own copy or localizations.
28 28
     cattr_accessor :default_error_messages
@@ -399,9 +399,11 @@ def validates_presence_of(*attr_names)
399 399
       # method, proc or string should return or evaluate to a true or false value.
400 400
       def validates_length_of(*attrs)
401 401
         # Merge given options with defaults.
402  
-        options = {:too_long     => ActiveRecord::Errors.default_error_messages[:too_long],
403  
-                   :too_short    => ActiveRecord::Errors.default_error_messages[:too_short],
404  
-                   :wrong_length => ActiveRecord::Errors.default_error_messages[:wrong_length]}.merge(DEFAULT_VALIDATION_OPTIONS)
  402
+        options = {
  403
+          :too_long     => ActiveRecord::Errors.default_error_messages[:too_long],
  404
+          :too_short    => ActiveRecord::Errors.default_error_messages[:too_short],
  405
+          :wrong_length => ActiveRecord::Errors.default_error_messages[:wrong_length]
  406
+        }.merge(DEFAULT_VALIDATION_OPTIONS)
405 407
         options.update(attrs.pop.symbolize_keys) if attrs.last.is_a?(Hash)
406 408
 
407 409
         # Ensure that one and only one range option is specified.
@@ -419,24 +421,31 @@ def validates_length_of(*attrs)
419 421
         option = range_options.first
420 422
         option_value = options[range_options.first]
421 423
 
422  
-        # Declare different validations per option.
423  
-        validity_checks = { :is => "==", :minimum => ">=", :maximum => "<=" }
424  
-        message_options = { :is => :wrong_length, :minimum => :too_short, :maximum => :too_long }
425  
-
426 424
         case option
427 425
         when :within, :in
428  
-          raise ArgumentError, ':within must be a Range' unless option_value.is_a?(Range) # '
429  
-          (options_without_range = options.dup).delete(option)
430  
-          (options_with_minimum = options_without_range.dup).store(:minimum, option_value.begin)
431  
-          validates_length_of attrs, options_with_minimum
432  
-          (options_with_maximum = options_without_range.dup).store(:maximum, option_value.end)
433  
-          validates_length_of attrs, options_with_maximum
  426
+          raise ArgumentError, ":#{option} must be a Range" unless option_value.is_a?(Range) 
  427
+
  428
+          too_short = options[:too_short] % option_value.begin
  429
+          too_long  = options[:too_long]  % option_value.end
  430
+
  431
+          validates_each(attrs, options) do |record, attr, value|
  432
+            if value.nil? or value.size < option_value.begin
  433
+              record.errors.add(attr, too_short)
  434
+            elsif value.size > option_value.end
  435
+              record.errors.add(attr, too_long)
  436
+            end
  437
+          end
434 438
         when :is, :minimum, :maximum
435  
-          raise ArgumentError, ":#{option} must be a nonnegative Integer" unless option_value.is_a?(Integer) and option_value >= 0 # '
436  
-          message = options[:message] || options[message_options[option]]
437  
-          message = (message % option_value) rescue message
  439
+          raise ArgumentError, ":#{option} must be a nonnegative Integer" unless option_value.is_a?(Integer) and option_value >= 0
  440
+
  441
+          # Declare different validations per option.
  442
+          validity_checks = { :is => "==", :minimum => ">=", :maximum => "<=" }
  443
+          message_options = { :is => :wrong_length, :minimum => :too_short, :maximum => :too_long }
  444
+
  445
+          message = (options[:message] || options[message_options[option]]) % option_value
  446
+
438 447
           validates_each(attrs, options) do |record, attr, value|
439  
-            record.errors.add(attr, message) if value.nil? or !value.size.method(validity_checks[option])[option_value]
  448
+            record.errors.add(attr, message) unless !value.nil? and value.size.method(validity_checks[option])[option_value]
440 449
           end
441 450
         end
442 451
       end
@@ -605,7 +614,7 @@ def validates_associated(*attr_names)
605 614
       # * <tt>message</tt> - A custom error message (default is: "is not a number")
606 615
       # * <tt>on</tt> Specifies when this validation is active (default is :save, other options :create, :update)
607 616
       # * <tt>only_integer</tt> Specifies whether the value has to be an integer, e.g. an integral value (default is false)
608  
-      # * <tt>allow_nil</tt> Skip validation if attribute is nil (default is false). Notice that for fixnum and float columsn empty strings are converted to nil
  617
+      # * <tt>allow_nil</tt> Skip validation if attribute is nil (default is false). Notice that for fixnum and float columns empty strings are converted to nil
609 618
       # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should
610 619
       # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }).  The
611 620
       # method, proc or string should return or evaluate to a true or false value.

0 notes on commit 6d7551f

Please sign in to comment.
Something went wrong with that request. Please try again.