1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add messages_for

This commit is contained in:
lulalala 2018-04-03 10:52:36 +08:00
parent cccbac6df6
commit 2a06f13099

View file

@ -65,6 +65,8 @@ module ActiveModel
extend Forwardable
def_delegators :@errors, :size, :clear, :blank?, :empty?
# TODO: forward all enumerable methods after `each` deprecation is removed.
def_delegators :@errors, :count
LEGACY_ATTRIBUTES = [:messages, :details].freeze
@ -199,7 +201,7 @@ module ActiveModel
# person.errors[:name] # => ["cannot be nil"]
# person.errors['name'] # => ["cannot be nil"]
def [](attribute)
where(attribute.to_sym).map { |error| error.message }
messages_for(attribute)
end
# Iterates through each error key, value pair in the error messages hash.
@ -604,6 +606,10 @@ module ActiveModel
def deprecation_removal_warning(method_name)
ActiveSupport::Deprecation.warn("ActiveModel::Errors##{method_name} is deprecated and will be removed in Rails 6.1")
end
def deprecation_rename_warning(old_method_name, new_method_name)
ActiveSupport::Deprecation.warn("ActiveModel::Errors##{old_method_name} is deprecated. Please call ##{new_method_name} instead.")
end
end
# Raised when a validation cannot be corrected by end users and are considered