Ticket #9574: validates_length_of_in.diff
| File validates_length_of_in.diff, 9.8 kB (added by lawrence, 1 year ago) |
|---|
-
activerecord/test/validations_test.rb
old new 568 568 assert t.valid? 569 569 end 570 570 571 def test_validates_length_of_using_in 572 Topic.validates_length_of(:title, :content, :in => 3..5) 573 574 t = Topic.new("title" => "a!", "content" => "I'm ooooooooh so very long") 575 assert !t.valid? 576 assert_equal "is too short (minimum is 3 characters)", t.errors.on(:title) 577 assert_equal "is too long (maximum is 5 characters)", t.errors.on(:content) 578 579 t.title = nil 580 t.content = nil 581 assert !t.valid? 582 assert_equal "is too short (minimum is 3 characters)", t.errors.on(:title) 583 assert_equal "is too short (minimum is 3 characters)", t.errors.on(:content) 584 585 t.title = "abe" 586 t.content = "mad" 587 assert t.valid? 588 end 589 571 590 def test_optionally_validates_length_of_using_within 572 591 Topic.validates_length_of :title, :content, :within => 3..5, :allow_nil => true 573 592 … … 684 703 assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within=>6, :maximum=>9) } 685 704 assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within=>6, :minimum=>9) } 686 705 assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within=>6, :is=>9) } 706 assert_raise(ArgumentError) { Topic.validates_length_of(:title, :minimum=>-1) } 707 assert_raise(ArgumentError) { Topic.validates_length_of(:title, :maximum=>-1) } 708 assert_raise(ArgumentError) { Topic.validates_length_of(:title, :is=>-1) } 687 709 assert_raise(ArgumentError) { Topic.validates_length_of(:title, :minimum=>"a") } 688 710 assert_raise(ArgumentError) { Topic.validates_length_of(:title, :maximum=>"a") } 689 assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within=>"a" ) }711 assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within=>"a".."z") } 690 712 assert_raise(ArgumentError) { Topic.validates_length_of(:title, :is=>"a") } 691 713 end 692 714 715 def test_validates_length_of_range_option_provided 716 assert_raise(ArgumentError) { Topic.validates_length_of(:title) } 717 end 718 719 def test_validates_length_of_custom_errors_for_in_with_message 720 Topic.validates_length_of( :title, :in=>2..5, :message=>"booin' must be between 2 and 5 characters" ) 721 t = Topic.create("title" => "uhohuhoh", "content" => "whatever") 722 assert !t.valid? 723 assert t.errors.on(:title) 724 assert_equal "booin' must be between 2 and 5 characters", t.errors["title"] 725 end 726 693 727 def test_validates_length_of_custom_errors_for_minimum_with_message 694 728 Topic.validates_length_of( :title, :minimum=>5, :message=>"boo %d" ) 695 729 t = Topic.create("title" => "uhoh", "content" => "whatever") -
activerecord/lib/active_record/validations.rb
old new 479 479 480 480 # can't use validates_each here, because it cannot cope with nonexistent attributes, 481 481 # while errors.add_on_empty can 482 send(validation_method(configuration[:on])) do |record|483 unless (configuration[:if] && !evaluate_condition(configuration[:if], record)) || (configuration[:unless] && evaluate_condition(configuration[:unless], record))484 record.errors.add_on_blank(attr_names, configuration[:message])485 end486 end482 send(validation_method(configuration[:on])) do |record| 483 unless (configuration[:if] && !evaluate_condition(configuration[:if], record)) || (configuration[:unless] && evaluate_condition(configuration[:unless], record)) 484 record.errors.add_on_blank(attr_names, configuration[:message]) 485 end 486 end 487 487 end 488 488 489 # Validates that the specified attribute matches the length restrictions supplied. Only one option can be used at a time: 489 # Validates that the specified attribute matches the length restriction supplied. 490 # Only one length restriction can be used at a time: 490 491 # 491 492 # class Person < ActiveRecord::Base 492 493 # validates_length_of :first_name, :maximum=>30 493 494 # validates_length_of :last_name, :maximum=>30, :message=>"less than %d if you don't mind" 494 495 # validates_length_of :fax, :in => 7..32, :allow_nil => true 495 496 # validates_length_of :phone, :in => 7..32, :allow_blank => true 496 # validates_length_of :user_name, : within => 6..20, :too_long => "pick a shorter name", :too_short => "pick a longer name"497 # validates_length_of :user_name, :in => 6..20, :too_long => "pick a shorter name", :too_short => "pick a longer name" 497 498 # validates_length_of :fav_bra_size, :minimum=>1, :too_short=>"please enter at least %d character" 498 499 # validates_length_of :smurf_leader, :is=>4, :message=>"papa is spelled with %d characters... don't play me." 499 500 # end 500 501 # 501 # Configuration options:502 # Length restriction configuration options: 502 503 # * <tt>minimum</tt> - The minimum size of the attribute 503 504 # * <tt>maximum</tt> - The maximum size of the attribute 504 505 # * <tt>is</tt> - The exact size of the attribute 505 # * <tt>within</tt> - A range specifying the minimum and maximum size of the attribute 506 # * <tt>in</tt> - A synonym(or alias) for :within 506 # * <tt>in</tt> - A range specifying the minimum and maximum size of the attribute 507 # 508 # Other configuration options: 507 509 # * <tt>allow_nil</tt> - Attribute may be nil; skip validation. 508 510 # * <tt>allow_blank</tt> - Attribute may be blank; skip validation. 509 #510 511 # * <tt>too_long</tt> - The error message if the attribute goes over the maximum (default is: "is too long (maximum is %d characters)") 511 512 # * <tt>too_short</tt> - The error message if the attribute goes under the minimum (default is: "is too short (min is %d characters)") 512 513 # * <tt>wrong_length</tt> - The error message if using the :is method and the attribute is the wrong size (default is: "is the wrong length (should be %d characters)") 513 # * <tt>message</tt> - The error message to use for a :minimum, :maximum, or :is violation. An alias of the appropriate too_long/too_short/wrong_length message514 # * <tt>message</tt> - Specifies a custom error message (default is nil). When defined it overrides the too_long/too_short/wrong_length messages. 514 515 # * <tt>on</tt> - Specifies when this validation is active (default is :save, other options :create, :update) 515 516 # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 516 517 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The 517 518 # method, proc or string should return or evaluate to a true or false value. 518 519 # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should 519 520 # not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). The 520 # method, proc or string should return or evaluate to a true or false value. 521 # method, proc or string should return or evaluate to a true or false value. 521 522 def validates_length_of(*attrs) 522 523 # Merge given options with defaults. 523 524 options = { … … 531 532 range_options = ALL_RANGE_OPTIONS & options.keys 532 533 case range_options.size 533 534 when 0 534 raise ArgumentError, 'Range unspecified. Specify the : within, :maximum, :minimum, or :is option.'535 raise ArgumentError, 'Range unspecified. Specify the :in, :maximum, :minimum, or :is option.' 535 536 when 1 536 537 # Valid number of options; do nothing. 537 538 else … … 543 544 option_value = options[range_options.first] 544 545 545 546 case option 546 when : within, :in547 when :in, :within 547 548 raise ArgumentError, ":#{option} must be a Range" unless option_value.is_a?(Range) 548 549 549 550 too_short = options[:too_short] % option_value.begin … … 551 552 552 553 validates_each(attrs, options) do |record, attr, value| 553 554 if value.nil? or value.split(//).size < option_value.begin 554 record.errors.add(attr, too_short)555 record.errors.add(attr, options[:message] || too_short) 555 556 elsif value.split(//).size > option_value.end 556 record.errors.add(attr, too_long)557 record.errors.add(attr, options[:message] || too_long) 557 558 end 558 559 end 559 560 when :is, :minimum, :maximum … … 693 694 694 695 enum = configuration[:in] || configuration[:within] 695 696 696 raise(ArgumentError, "An object with the method include? is requiredmust be supplied as the :in option of the configuration hash") unless enum.respond_to?("include?")697 raise(ArgumentError, "An object with the method include? must be supplied as the :in option of the configuration hash") unless enum.respond_to?("include?") 697 698 698 699 validates_each(attr_names, configuration) do |record, attr_name, value| 699 700 record.errors.add(attr_name, configuration[:message]) unless enum.include?(value) … … 724 725 725 726 enum = configuration[:in] || configuration[:within] 726 727 727 raise(ArgumentError, "An object with the method include? is requiredmust be supplied as the :in option of the configuration hash") unless enum.respond_to?("include?")728 raise(ArgumentError, "An object with the method include? must be supplied as the :in option of the configuration hash") unless enum.respond_to?("include?") 728 729 729 730 validates_each(attr_names, configuration) do |record, attr_name, value| 730 731 record.errors.add(attr_name, configuration[:message]) if enum.include?(value)