Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 7407

Show
Ignore:
Timestamp:
09/05/07 05:37:52 (2 years ago)
Author:
nzkoz
Message:

Add :allow_blank to validations. Like allow_nil, but for values which are +blank?+. [jnoon] Closes #7383

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/lib/active_record/validations.rb

    r7402 r7407  
    289289        :on => :save, 
    290290        :allow_nil => false, 
     291        :allow_blank => false, 
    291292        :message => nil 
    292293      }.freeze 
     
    347348      # * <tt>on</tt> - Specifies when this validation is active (default is :save, other options :create, :update) 
    348349      # * <tt>allow_nil</tt> - Skip validation if attribute is nil. 
     350      # * <tt>allow_blank</tt> - Skip validation if attribute is blank. 
    349351      # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 
    350352      #   occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }).  The 
     
    363365            attrs.each do |attr| 
    364366              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]) 
    366368              yield record, attr, value 
    367369            end 
     
    491493      #     validates_length_of :last_name, :maximum=>30, :message=>"less than %d if you don't mind" 
    492494      #     validates_length_of :fax, :in => 7..32, :allow_nil => true 
     495      #     validates_length_of :phone, :in => 7..32, :allow_blank => true 
    493496      #     validates_length_of :user_name, :within => 6..20, :too_long => "pick a shorter name", :too_short => "pick a longer name" 
    494497      #     validates_length_of :fav_bra_size, :minimum=>1, :too_short=>"please enter at least %d character" 
     
    503506      # * <tt>in</tt> - A synonym(or alias) for :within 
    504507      # * <tt>allow_nil</tt> - Attribute may be nil; skip validation. 
     508      # * <tt>allow_blank</tt> - Attribute may be blank; skip validation. 
    505509      # 
    506510      # * <tt>too_long</tt> - The error message if the attribute goes over the maximum (default is: "is too long (maximum is %d characters)") 
     
    597601      # * <tt>case_sensitive</tt> - Looks for an exact match.  Ignored by non-text columns (true by default). 
    598602      # * <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) 
    599604      # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 
    600605      #   occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }).  The 
     
    676681      # * <tt>message</tt> - Specifies a customer error message (default is: "is not included in the list") 
    677682      # * <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) 
    678684      # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 
    679685      #   occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }).  The 
     
    706712      # * <tt>message</tt> - Specifies a customer error message (default is: "is reserved") 
    707713      # * <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) 
    708715      # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 
    709716      #   occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }).  The 
  • trunk/activerecord/test/validations_test.rb

    r7402 r7407  
    458458  end 
    459459 
     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 
    460478  def test_numericality_with_allow_nil_and_getter_method 
    461479    Developer.validates_numericality_of( :salary, :allow_nil => true)