aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/errors.rb
diff options
context:
space:
mode:
authorBogdan Gusiev <agresso@gmail.com>2012-08-06 13:45:27 +0300
committerBogdan Gusiev <agresso@gmail.com>2012-08-06 13:45:27 +0300
commit2e4f7986b8ec90d7b41c385388be21b8cee79b9c (patch)
treed7dd64e1c97b9dcf1c263ebd2bd408b7fd7c9208 /activemodel/lib/active_model/errors.rb
parent5edfc463484827df364a1e589677d5c84dfac282 (diff)
downloadrails-2e4f7986b8ec90d7b41c385388be21b8cee79b9c.tar.gz
rails-2e4f7986b8ec90d7b41c385388be21b8cee79b9c.tar.bz2
rails-2e4f7986b8ec90d7b41c385388be21b8cee79b9c.zip
AM::Validation#validates: ability to pass custom exception to `:strict` option
Diffstat (limited to 'activemodel/lib/active_model/errors.rb')
-rw-r--r--activemodel/lib/active_model/errors.rb8
1 files changed, 6 insertions, 2 deletions
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb
index 1026b0f4d3..b3b9ba8e56 100644
--- a/activemodel/lib/active_model/errors.rb
+++ b/activemodel/lib/active_model/errors.rb
@@ -283,15 +283,19 @@ module ActiveModel
#
# If the <tt>:strict</tt> option is set to true will raise
# ActiveModel::StrictValidationFailed instead of adding the error.
+ # <tt>:strict</tt> option can also be set to any other exception.
#
# person.errors.add(:name, nil, strict: true)
# # => ActiveModel::StrictValidationFailed: name is invalid
+ # person.errors.add(:name, nil, strict: NameIsInvalid)
+ # # => NameIsInvalid: name is invalid
#
# person.errors.messages # => {}
def add(attribute, message = nil, options = {})
message = normalize_message(attribute, message, options)
- if options[:strict]
- raise ActiveModel::StrictValidationFailed, full_message(attribute, message)
+ if exception = options[:strict]
+ exception = ActiveModel::StrictValidationFailed if exception == true
+ raise exception, full_message(attribute, message)
end
self[attribute] << message