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

Allow to override the full_message error format

This commit is contained in:
Martin Larochelle 2018-05-17 08:39:31 -04:00
parent 5003d5996a
commit 7d09874a71
2 changed files with 77 additions and 2 deletions

View file

@ -364,12 +364,51 @@ module ActiveModel
# Returns a full message for a given attribute.
#
# person.errors.full_message(:name, 'is invalid') # => "Name is invalid"
#
# The `"%{attribute} %{message}"` error format can be overridden with either
#
# * <tt>activemodel.errors.models.person/contacts/addresses.attributes.street.format</tt>
# * <tt>activemodel.errors.models.person/contacts/addresses.format</tt>
# * <tt>activemodel.errors.models.person.attributes.name.format</tt>
# * <tt>activemodel.errors.models.person.format</tt>
# * <tt>errors.format</tt>
def full_message(attribute, message)
return message if attribute == :base
if @base.class.respond_to?(:i18n_scope)
parts = attribute.to_s.split(".")
attribute_name = parts.pop
namespace = parts.join("/") unless parts.empty?
attributes_scope = "#{@base.class.i18n_scope}.errors.models"
if namespace
defaults = @base.class.lookup_ancestors.map do |klass|
[
:"#{attributes_scope}.#{klass.model_name.i18n_key}/#{namespace}.attributes.#{attribute_name}.format",
:"#{attributes_scope}.#{klass.model_name.i18n_key}/#{namespace}.format",
]
end
else
defaults = @base.class.lookup_ancestors.map do |klass|
[
:"#{attributes_scope}.#{klass.model_name.i18n_key}.attributes.#{attribute_name}.format",
:"#{attributes_scope}.#{klass.model_name.i18n_key}.format",
]
end
end
defaults.flatten!
else
defaults = []
end
defaults << :"errors.format"
defaults << "%{attribute} %{message}"
attr_name = attribute.to_s.tr(".", "_").humanize
attr_name = @base.class.human_attribute_name(attribute, default: attr_name)
I18n.t(:"errors.format",
default: "%{attribute} %{message}",
I18n.t(defaults.shift,
default: defaults,
attribute: attr_name,
message: message)
end

View file

@ -42,6 +42,42 @@ class I18nValidationTest < ActiveModel::TestCase
assert_equal ["Field Name empty"], @person.errors.full_messages
end
def test_errors_full_messages_uses_attribute_format
I18n.backend.store_translations("en", activemodel: {
errors: { models: { person: { attributes: { name: { format: "%{message}" } } } } } })
person = Person.new
assert_equal "cannot be blank", person.errors.full_message(:name, "cannot be blank")
assert_equal "Name test cannot be blank", person.errors.full_message(:name_test, "cannot be blank")
end
def test_errors_full_messages_uses_model_format
I18n.backend.store_translations("en", activemodel: {
errors: { models: { person: { format: "%{message}" } } } })
person = Person.new
assert_equal "cannot be blank", person.errors.full_message(:name, "cannot be blank")
assert_equal "cannot be blank", person.errors.full_message(:name_test, "cannot be blank")
end
def test_errors_full_messages_uses_deeply_nested_model_attributes_format
I18n.backend.store_translations("en", activemodel: {
errors: { models: { 'person/contacts/addresses': { attributes: { street: { format: "%{message}" } } } } } })
person = Person.new
assert_equal "cannot be blank", person.errors.full_message(:'contacts/addresses.street', "cannot be blank")
assert_equal "Contacts/addresses country cannot be blank", person.errors.full_message(:'contacts/addresses.country', "cannot be blank")
end
def test_errors_full_messages_uses_deeply_nested_model_model_format
I18n.backend.store_translations("en", activemodel: {
errors: { models: { 'person/contacts/addresses': { format: "%{message}" } } } })
person = Person.new
assert_equal "cannot be blank", person.errors.full_message(:'contacts/addresses.street', "cannot be blank")
assert_equal "cannot be blank", person.errors.full_message(:'contacts/addresses.country', "cannot be blank")
end
# ActiveModel::Validations
# A set of common cases for ActiveModel::Validations message generation that