Added Errors#add_on_blank which works like Errors#add_on_empty, but uses Object#blank? instead. CHANGED: validates_presence_of now uses Errors#add_on_blank, which will make " " fail the validation where it didnt before #1309. Added that " " is now also blank? (using strip if available)

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1346 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
David Heinemeier Hansson 2005-05-21 18:12:36 +00:00
parent 7159402c6d
commit a1e9ceebd5
5 changed files with 33 additions and 10 deletions

@ -1,5 +1,9 @@
*SVN*
* CHANGED: validates_presence_of now uses Errors#add_on_blank, which will make " " fail the validation where it didn't before #1309
* Added Errors#add_on_blank which works like Errors#add_on_empty, but uses Object#blank? instead
* Added the :if option to all validations that can either use a block or a method pointer to determine whether the validation should be run or not. #1324 [Duane Johnson/jhosteny]. Examples:
Conditional validations such as the following are made possible:

@ -16,6 +16,7 @@ def initialize(base) # :nodoc:
:confirmation => "doesn't match confirmation",
:accepted => "must be accepted",
:empty => "can't be empty",
:blank => "can't be blank",
:too_long => "is too long (max is %d characters)",
:too_short => "is too short (min is %d characters)",
:wrong_length => "is the wrong length (should be %d characters)",
@ -44,7 +45,7 @@ def add(attribute, msg = @@default_error_messages[:invalid])
@errors[attribute.to_s] << msg
end
# Will add an error message to each of the attributes in +attributes+ that is empty (defined by <tt>attribute_present?</tt>).
# Will add an error message to each of the attributes in +attributes+ that is empty.
def add_on_empty(attributes, msg = @@default_error_messages[:empty])
for attr in [attributes].flatten
value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s]
@ -52,6 +53,14 @@ def add_on_empty(attributes, msg = @@default_error_messages[:empty])
add(attr, msg) unless !value.nil? && !is_empty
end
end
# Will add an error message to each of the attributes in +attributes+ that is blank (using Object#blank?).
def add_on_blank(attributes, msg = @@default_error_messages[:blank])
for attr in [attributes].flatten
value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s]
add(attr, msg) if value.blank?
end
end
# Will add an error message to each of the attributes in +attributes+ that has a length outside of the passed boundary +range+.
# If the length is above the boundary, the too_long_msg message will be used. If below, the too_short_msg.
@ -325,7 +334,7 @@ def validates_confirmation_of(*attr_names)
# terms_of_service is not nil and by default on save.
#
# Configuration options:
# * <tt>message</tt> - A custom error message (default is: "can't be empty")
# * <tt>message</tt> - A custom error message (default is: "must be accepted")
# * <tt>on</tt> - Specifies when this validation is active (default is :save, other options :create, :update)
# * <tt>accept</tt> - Specifies value that is considered accepted. The default value is a string "1", which
# makes it easy to relate to an HTML checkbox.
@ -343,16 +352,16 @@ def validates_acceptance_of(*attr_names)
end
end
# Validates that the specified attributes are neither nil nor empty. Happens by default on save.
# Validates that the specified attributes are not blank (as defined by Object#blank?). Happens by default on save.
#
# Configuration options:
# * <tt>message</tt> - A custom error message (default is: "has already been taken")
# * <tt>message</tt> - A custom error message (default is: "can't be blank")
# * <tt>on</tt> - Specifies when this validation is active (default is :save, other options :create, :update)
# * <tt>if</tt> - 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.
def validates_presence_of(*attr_names)
configuration = { :message => ActiveRecord::Errors.default_error_messages[:empty], :on => :save }
configuration = { :message => ActiveRecord::Errors.default_error_messages[:blank], :on => :save }
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
# can't use validates_each here, because it cannot cope with non-existant attributes,
@ -360,7 +369,7 @@ def validates_presence_of(*attr_names)
attr_names.each do |attr_name|
send(validation_method(configuration[:on])) do |record|
unless configuration[:if] and not evaluate_condition(configuration[:if], record)
record.errors.add_on_empty(attr_name,configuration[:message])
record.errors.add_on_blank(attr_name,configuration[:message])
end
end
end

@ -221,11 +221,16 @@ def test_validate_presences
t = Topic.create
assert !t.save
assert_equal "can't be empty", t.errors.on(:title)
assert_equal "can't be empty", t.errors.on(:content)
assert_equal "can't be blank", t.errors.on(:title)
assert_equal "can't be blank", t.errors.on(:content)
t.title = "something"
t.content = "another"
t.content = " "
assert !t.save
assert_equal "can't be blank", t.errors.on(:content)
t.content = "like stuff"
assert t.save
end

@ -1,5 +1,7 @@
*SVN*
* Added that " " is now also blank? (using strip if available)
* Fixed Dependencies so all modules are able to load missing constants #1173 [Nicholas Seckar]
* Fixed the Inflector to underscore strings containing numbers, so Area51Controller becomes area51_controller #1176 [Nicholas Seckar]

@ -12,8 +12,11 @@ def subclasses_of(superclass)
subclasses
end
# "", " ", nil, and 0 are all blank
def blank?
if respond_to? :empty?
if respond_to?(:empty?) && respond_to?(:strip)
strip.empty?
elsif respond_to? :empty?
empty?
elsif respond_to? :zero?
zero?