Skip to content
This repository
Browse code

Fixed that validate_length_of lost :on option when :within was specif…

…ied #1195 [jhosteny@mac.com]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1258 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 9f1b57779ff3db4f7ef6bfb9e7d1670972340896 1 parent 537efa3
David Heinemeier Hansson authored April 30, 2005
2  activerecord/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* Fixed that validate_length_of lost :on option when :within was specified #1195 [jhosteny@mac.com]
  4
+
3 5
 * Added encoding and min_messages options for PostgreSQL #1205 [shugo]. Configuration example:
4 6
 
5 7
     development:
8  activerecord/lib/active_record/validations.rb
@@ -373,15 +373,17 @@ def validates_length_of(*attrs)
373 373
         option_value = options[range_options.first]
374 374
 
375 375
         # Declare different validations per option.
376  
-        
377 376
         validity_checks = { :is => "==", :minimum => ">=", :maximum => "<=" }
378 377
         message_options = { :is => :wrong_length, :minimum => :too_short, :maximum => :too_long }
379 378
 
380 379
         case option
381 380
         when :within, :in
382 381
           raise ArgumentError, ':within must be a Range' unless option_value.is_a?(Range) # '
383  
-          validates_length_of attrs, :minimum => option_value.begin, :allow_nil => options[:allow_nil]
384  
-          validates_length_of attrs, :maximum => option_value.end, :allow_nil => options[:allow_nil]
  382
+          (options_without_range = options.dup).delete(option)
  383
+          (options_with_minimum = options_without_range.dup).store(:minimum, option_value.begin)
  384
+          validates_length_of attrs, options_with_minimum
  385
+          (options_with_maximum = options_without_range.dup).store(:maximum, option_value.end)
  386
+          validates_length_of attrs, options_with_maximum
385 387
         when :is, :minimum, :maximum
386 388
           raise ArgumentError, ":#{option} must be a nonnegative Integer" unless option_value.is_a?(Integer) and option_value >= 0 # '
387 389
           message = options[:message] || options[message_options[option]]
42  activerecord/test/validations_test.rb
@@ -394,6 +394,48 @@ def test_optionally_validates_length_of_using_within
394 394
     assert t.valid?
395 395
   end
396 396
 
  397
+  def test_optionally_validates_length_of_using_within_on_create
  398
+    Topic.validates_length_of :title, :content, :within => 5..10, :on => :create, :too_long => "my string is too long: %d"
  399
+
  400
+    t = Topic.create("title" => "thisisnotvalid", "content" => "whatever")
  401
+    assert !t.save
  402
+    assert t.errors.on(:title)
  403
+    assert_equal "my string is too long: 10", t.errors[:title]
  404
+
  405
+    t.title = "butthisis"
  406
+    assert t.save
  407
+
  408
+    t.title = "few"
  409
+    assert t.save
  410
+
  411
+    t.content = "andthisislong"
  412
+    assert t.save
  413
+
  414
+    t.content = t.title = "iamfine"
  415
+    assert t.save
  416
+  end
  417
+
  418
+  def test_optionally_validates_length_of_using_within_on_update
  419
+    Topic.validates_length_of :title, :content, :within => 5..10, :on => :update, :too_short => "my string is too short: %d"
  420
+
  421
+    t = Topic.create("title" => "vali", "content" => "whatever")
  422
+    assert !t.save
  423
+    assert t.errors.on(:title)
  424
+
  425
+    t.title = "not"
  426
+    assert !t.save
  427
+    assert t.errors.on(:title)
  428
+    assert_equal "my string is too short: 5", t.errors[:title]
  429
+
  430
+    t.title = "valid"
  431
+    t.content = "andthisistoolong"
  432
+    assert !t.save
  433
+    assert t.errors.on(:content)
  434
+
  435
+    t.content = "iamfine"
  436
+    assert t.save
  437
+  end
  438
+
397 439
   def test_validates_length_of_using_is
398 440
     Topic.validates_length_of :title, :is => 5
399 441
 

0 notes on commit 9f1b577

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