aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-05-21 18:12:36 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-05-21 18:12:36 +0000
commita1e9ceebd59e9ac6b932f7ed5d35fe44eee6b93f (patch)
treef907e976a5d0093bea013a98422dee2efd97e4d8 /activerecord
parent7159402c6d0edab20ff84a686bcd03894b4e9c4c (diff)
downloadrails-a1e9ceebd59e9ac6b932f7ed5d35fe44eee6b93f.tar.gz
rails-a1e9ceebd59e9ac6b932f7ed5d35fe44eee6b93f.tar.bz2
rails-a1e9ceebd59e9ac6b932f7ed5d35fe44eee6b93f.zip
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
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG4
-rwxr-xr-xactiverecord/lib/active_record/validations.rb21
-rwxr-xr-xactiverecord/test/validations_test.rb11
3 files changed, 27 insertions, 9 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 4a0b0df361..5769b84d14 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -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:
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb
index 052484c365..3eedd0093c 100755
--- a/activerecord/lib/active_record/validations.rb
+++ b/activerecord/lib/active_record/validations.rb
@@ -16,6 +16,7 @@ module ActiveRecord
: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 @@ module ActiveRecord
@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 @@ module ActiveRecord
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 @@ module ActiveRecord
# 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 @@ module ActiveRecord
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 @@ module ActiveRecord
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
diff --git a/activerecord/test/validations_test.rb b/activerecord/test/validations_test.rb
index 13c4a04b62..2cce21597a 100755
--- a/activerecord/test/validations_test.rb
+++ b/activerecord/test/validations_test.rb
@@ -221,11 +221,16 @@ class ValidationsTest < Test::Unit::TestCase
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