aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@plataformatec.com.br>2012-02-01 05:50:37 -0800
committerJosé Valim <jose.valim@plataformatec.com.br>2012-02-01 05:50:37 -0800
commit3287a6e2bd505a6d70359edd46950206a0854359 (patch)
tree1d91c0e7bf09e42238939b471a7901de21414780
parent0b5edb32cf53873c975f0c656f356dd3f440233c (diff)
parent26861e95064c57159486a02709ca0f2436596062 (diff)
downloadrails-3287a6e2bd505a6d70359edd46950206a0854359.tar.gz
rails-3287a6e2bd505a6d70359edd46950206a0854359.tar.bz2
rails-3287a6e2bd505a6d70359edd46950206a0854359.zip
Merge pull request #4821 from carlosantoniodasilva/strict-validation
Generate strict validation error messages with attribute name
-rw-r--r--activemodel/lib/active_model/errors.rb2
-rw-r--r--activemodel/lib/active_model/validations/validates.rb13
-rw-r--r--activemodel/test/cases/validations_test.rb15
3 files changed, 19 insertions, 11 deletions
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb
index 023c872055..75feba1fe7 100644
--- a/activemodel/lib/active_model/errors.rb
+++ b/activemodel/lib/active_model/errors.rb
@@ -224,7 +224,7 @@ module ActiveModel
def add(attribute, message = nil, options = {})
message = normalize_message(attribute, message, options)
if options[:strict]
- raise ActiveModel::StrictValidationFailed, message
+ raise ActiveModel::StrictValidationFailed, full_message(attribute, message)
end
self[attribute] << message
diff --git a/activemodel/lib/active_model/validations/validates.rb b/activemodel/lib/active_model/validations/validates.rb
index 3713fc828e..9bb72d6631 100644
--- a/activemodel/lib/active_model/validations/validates.rb
+++ b/activemodel/lib/active_model/validations/validates.rb
@@ -1,7 +1,6 @@
require 'active_support/core_ext/hash/slice'
module ActiveModel
-
# == Active Model validates method
module Validations
module ClassMethods
@@ -101,11 +100,11 @@ module ActiveModel
end
end
- # This method is used to define validation that can not be corrected by end user
- # and is considered exceptional.
- # So each validator defined with bang or <tt>:strict</tt> option set to <tt>true</tt>
- # will always raise <tt>ActiveModel::InternalValidationFailed</tt> instead of adding error
- # when validation fails
+ # This method is used to define validation that cannot be corrected by end
+ # user and is considered exceptional. So each validator defined with bang
+ # or <tt>:strict</tt> option set to <tt>true</tt> will always raise
+ # <tt>ActiveModel::StrictValidationFailed</tt> instead of adding error
+ # when validation fails.
# See <tt>validates</tt> for more information about validation itself.
def validates!(*attributes)
options = attributes.extract_options!
@@ -118,7 +117,7 @@ module ActiveModel
# When creating custom validators, it might be useful to be able to specify
# additional default keys. This can be done by overwriting this method.
def _validates_default_keys
- [ :if, :unless, :on, :allow_blank, :allow_nil , :strict]
+ [:if, :unless, :on, :allow_blank, :allow_nil , :strict]
end
def _parse_validates_options(options) #:nodoc:
diff --git a/activemodel/test/cases/validations_test.rb b/activemodel/test/cases/validations_test.rb
index fe5358a9d0..0b1de62a48 100644
--- a/activemodel/test/cases/validations_test.rb
+++ b/activemodel/test/cases/validations_test.rb
@@ -310,7 +310,7 @@ class ValidationsTest < ActiveModel::TestCase
end
def test_strict_validation_particular_validator
- Topic.validates :title, :presence => {:strict => true}
+ Topic.validates :title, :presence => { :strict => true }
assert_raises ActiveModel::StrictValidationFailed do
Topic.new.valid?
end
@@ -330,9 +330,18 @@ class ValidationsTest < ActiveModel::TestCase
end
end
+ def test_strict_validation_error_message
+ Topic.validates :title, :strict => true, :presence => true
+
+ exception = assert_raises(ActiveModel::StrictValidationFailed) do
+ Topic.new.valid?
+ end
+ assert_equal "Title can't be blank", exception.message
+ end
+
def test_does_not_modify_options_argument
- options = {:presence => true}
+ options = { :presence => true }
Topic.validates :title, options
- assert_equal({:presence => true}, options)
+ assert_equal({ :presence => true }, options)
end
end