Skip to content

Commit

Permalink
Generate strict validation error messages with attribute name
Browse files Browse the repository at this point in the history
Backported from master.
  • Loading branch information
carlosantoniodasilva committed Feb 1, 2012
1 parent 1c5bd8a commit 91f8cf2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def to_hash
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
Expand Down
17 changes: 8 additions & 9 deletions activemodel/lib/active_model/validations/validates.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'active_support/core_ext/hash/slice'

module ActiveModel

# == Active Model validates method
module Validations
module ClassMethods
Expand Down Expand Up @@ -59,7 +58,7 @@ module ClassMethods
#
# validates :name, :'film/title' => true
#
# The validators hash can also handle regular expressions, ranges,
# The validators hash can also handle regular expressions, ranges,
# arrays and strings in shortcut form, e.g.
#
# validates :email, :format => /@/
Expand All @@ -70,7 +69,7 @@ module ClassMethods
# validator's initializer as +options[:in]+ while other types including
# regular expressions and strings are passed as +options[:with]+
#
# Finally, the options +:if+, +:unless+, +:on+, +:allow_blank+, +:allow_nil+ and +:strict+
# Finally, the options +:if+, +:unless+, +:on+, +:allow_blank+, +:allow_nil+ and +:strict+
# can be given to one specific validator, as a hash:
#
# validates :password, :presence => { :if => :password_required? }, :confirmation => true
Expand Down Expand Up @@ -101,11 +100,11 @@ def validates(*attributes)
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!
Expand All @@ -118,7 +117,7 @@ def validates!(*attributes)
# 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:
Expand Down
11 changes: 10 additions & 1 deletion activemodel/test/cases/validations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def test_strict_validation_not_fails
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
Expand All @@ -330,4 +330,13 @@ def test_validates_with_bang
Topic.new.valid?
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
end

0 comments on commit 91f8cf2

Please sign in to comment.