diff options
Diffstat (limited to 'activemodel/lib/active_model')
-rw-r--r-- | activemodel/lib/active_model/errors.rb | 4 | ||||
-rw-r--r-- | activemodel/lib/active_model/validator.rb | 7 |
2 files changed, 9 insertions, 2 deletions
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index ea69e7549e..36834bbd36 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -327,7 +327,7 @@ module ActiveModel # # => {:base=>[{error: :name_or_email_blank}]} def add(attribute, message = :invalid, options = {}) message = message.call if message.respond_to?(:call) - detail = normalize_detail(attribute, message, options) + detail = normalize_detail(message, options) message = normalize_message(attribute, message, options) if exception = options[:strict] exception = ActiveModel::StrictValidationFailed if exception == true @@ -502,7 +502,7 @@ module ActiveModel end end - def normalize_detail(attribute, message, options) + def normalize_detail(message, options) { error: message }.merge(options.except(*CALLBACKS_OPTIONS + MESSAGE_OPTIONS)) end end diff --git a/activemodel/lib/active_model/validator.rb b/activemodel/lib/active_model/validator.rb index 1d2888a818..21339b628e 100644 --- a/activemodel/lib/active_model/validator.rb +++ b/activemodel/lib/active_model/validator.rb @@ -167,6 +167,13 @@ module ActiveModel def should_validate?(record) # :nodoc: !record.persisted? || record.changed? || record.marked_for_destruction? end + + # Always validate the record if the attribute is a virtual attribute. + # We have no way of knowing that the record was changed if the attribute + # does not exist in the database. + def unknown_attribute?(record, attribute) # :nodoc: + !record.attributes.include?(attribute.to_s) + end end # +BlockValidator+ is a special +EachValidator+ which receives a block on initialization |