Changeset 7407
- Timestamp:
- 09/05/07 05:37:52 (2 years ago)
- Files:
-
- trunk/activerecord/lib/active_record/validations.rb (modified) (8 diffs)
- trunk/activerecord/test/validations_test.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activerecord/lib/active_record/validations.rb
r7402 r7407 289 289 :on => :save, 290 290 :allow_nil => false, 291 :allow_blank => false, 291 292 :message => nil 292 293 }.freeze … … 347 348 # * <tt>on</tt> - Specifies when this validation is active (default is :save, other options :create, :update) 348 349 # * <tt>allow_nil</tt> - Skip validation if attribute is nil. 350 # * <tt>allow_blank</tt> - Skip validation if attribute is blank. 349 351 # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 350 352 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The … … 363 365 attrs.each do |attr| 364 366 value = record.send(attr) 365 next if value.nil? && options[:allow_nil]367 next if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank]) 366 368 yield record, attr, value 367 369 end … … 491 493 # validates_length_of :last_name, :maximum=>30, :message=>"less than %d if you don't mind" 492 494 # validates_length_of :fax, :in => 7..32, :allow_nil => true 495 # validates_length_of :phone, :in => 7..32, :allow_blank => true 493 496 # validates_length_of :user_name, :within => 6..20, :too_long => "pick a shorter name", :too_short => "pick a longer name" 494 497 # validates_length_of :fav_bra_size, :minimum=>1, :too_short=>"please enter at least %d character" … … 503 506 # * <tt>in</tt> - A synonym(or alias) for :within 504 507 # * <tt>allow_nil</tt> - Attribute may be nil; skip validation. 508 # * <tt>allow_blank</tt> - Attribute may be blank; skip validation. 505 509 # 506 510 # * <tt>too_long</tt> - The error message if the attribute goes over the maximum (default is: "is too long (maximum is %d characters)") … … 597 601 # * <tt>case_sensitive</tt> - Looks for an exact match. Ignored by non-text columns (true by default). 598 602 # * <tt>allow_nil</tt> - If set to true, skips this validation if the attribute is null (default is: false) 603 # * <tt>allow_blank</tt> - If set to true, skips this validation if the attribute is blank (default is: false) 599 604 # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 600 605 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The … … 676 681 # * <tt>message</tt> - Specifies a customer error message (default is: "is not included in the list") 677 682 # * <tt>allow_nil</tt> - If set to true, skips this validation if the attribute is null (default is: false) 683 # * <tt>allow_blank</tt> - If set to true, skips this validation if the attribute is blank (default is: false) 678 684 # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 679 685 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The … … 706 712 # * <tt>message</tt> - Specifies a customer error message (default is: "is reserved") 707 713 # * <tt>allow_nil</tt> - If set to true, skips this validation if the attribute is null (default is: false) 714 # * <tt>allow_blank</tt> - If set to true, skips this validation if the attribute is blank (default is: false) 708 715 # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 709 716 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The trunk/activerecord/test/validations_test.rb
r7402 r7407 458 458 end 459 459 460 def test_validates_length_of_with_allow_nil 461 Topic.validates_length_of( :title, :is => 5, :allow_nil=>true ) 462 463 assert !Topic.create("title" => "ab").valid? 464 assert !Topic.create("title" => "").valid? 465 assert Topic.create("title" => nil).valid? 466 assert Topic.create("title" => "abcde").valid? 467 end 468 469 def test_validates_length_of_with_allow_blank 470 Topic.validates_length_of( :title, :is => 5, :allow_blank=>true ) 471 472 assert !Topic.create("title" => "ab").valid? 473 assert Topic.create("title" => "").valid? 474 assert Topic.create("title" => nil).valid? 475 assert Topic.create("title" => "abcde").valid? 476 end 477 460 478 def test_numericality_with_allow_nil_and_getter_method 461 479 Developer.validates_numericality_of( :salary, :allow_nil => true)