From aa32f66ebba1172ea006ee815df396152cacdffe Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Wed, 5 Sep 2007 05:37:52 +0000 Subject: Add :allow_blank to validations. Like allow_nil, but for values which are +blank?+. [jnoon] Closes #7383 git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7407 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record/validations.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 7de391f6fa..c5c3a2b84a 100755 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -288,6 +288,7 @@ module ActiveRecord DEFAULT_VALIDATION_OPTIONS = { :on => :save, :allow_nil => false, + :allow_blank => false, :message => nil }.freeze @@ -346,6 +347,7 @@ module ActiveRecord # Options: # * on - Specifies when this validation is active (default is :save, other options :create, :update) # * allow_nil - Skip validation if attribute is nil. + # * allow_blank - Skip validation if attribute is blank. # * if - Specifies a method, proc or string to call to determine if the validation should # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The # method, proc or string should return or evaluate to a true or false value. @@ -362,7 +364,7 @@ module ActiveRecord unless (options[:if] && !evaluate_condition(options[:if], record)) || (options[:unless] && evaluate_condition(options[:unless], record)) attrs.each do |attr| value = record.send(attr) - next if value.nil? && options[:allow_nil] + next if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank]) yield record, attr, value end end @@ -490,6 +492,7 @@ module ActiveRecord # validates_length_of :first_name, :maximum=>30 # validates_length_of :last_name, :maximum=>30, :message=>"less than %d if you don't mind" # validates_length_of :fax, :in => 7..32, :allow_nil => true + # validates_length_of :phone, :in => 7..32, :allow_blank => true # validates_length_of :user_name, :within => 6..20, :too_long => "pick a shorter name", :too_short => "pick a longer name" # validates_length_of :fav_bra_size, :minimum=>1, :too_short=>"please enter at least %d character" # validates_length_of :smurf_leader, :is=>4, :message=>"papa is spelled with %d characters... don't play me." @@ -502,6 +505,7 @@ module ActiveRecord # * within - A range specifying the minimum and maximum size of the attribute # * in - A synonym(or alias) for :within # * allow_nil - Attribute may be nil; skip validation. + # * allow_blank - Attribute may be blank; skip validation. # # * too_long - The error message if the attribute goes over the maximum (default is: "is too long (maximum is %d characters)") # * too_short - The error message if the attribute goes under the minimum (default is: "is too short (min is %d characters)") @@ -596,6 +600,7 @@ module ActiveRecord # * scope - One or more columns by which to limit the scope of the uniquness constraint. # * case_sensitive - Looks for an exact match. Ignored by non-text columns (true by default). # * allow_nil - If set to true, skips this validation if the attribute is null (default is: false) + # * allow_blank - If set to true, skips this validation if the attribute is blank (default is: false) # * if - Specifies a method, proc or string to call to determine if the validation should # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The # method, proc or string should return or evaluate to a true or false value. @@ -675,6 +680,7 @@ module ActiveRecord # * in - An enumerable object of available items # * message - Specifies a customer error message (default is: "is not included in the list") # * allow_nil - If set to true, skips this validation if the attribute is null (default is: false) + # * allow_blank - If set to true, skips this validation if the attribute is blank (default is: false) # * if - Specifies a method, proc or string to call to determine if the validation should # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The # method, proc or string should return or evaluate to a true or false value. @@ -705,6 +711,7 @@ module ActiveRecord # * in - An enumerable object of items that the value shouldn't be part of # * message - Specifies a customer error message (default is: "is reserved") # * allow_nil - If set to true, skips this validation if the attribute is null (default is: false) + # * allow_blank - If set to true, skips this validation if the attribute is blank (default is: false) # * if - Specifies a method, proc or string to call to determine if the validation should # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The # method, proc or string should return or evaluate to a true or false value. -- cgit v1.2.3