Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

i18n error messages: custom formats by attribute #7369

Closed
wants to merge 1 commit into from

9 participants

@ey-pairing

First place lookup is now: errors.formats.attributes.#{attribute}

Example use case:

errors:
  attributes:
    last_name:
      blank: you must provide your full name
  formats:
    attributes:
      last_name: "%{message}"

(same fallbacks as before)

@ryansouza ryansouza i18n error messages: custom formats by attribute
First place lookup is now: errors.formats.attributes.#{attribute}

Example use case:

    errors:
      attributes:
        last_name:
          blank: you must provide your full name
      formats:
        attributes:
          last_name: "%{message}"

(same fallbacks as before)
5b815f3
@ey-pairing

This patch could also be reasonably be applied to 3.2-stable branch

@rafaelfranca rafaelfranca commented on the diff
activemodel/lib/active_model/errors.rb
@@ -355,8 +355,8 @@ def full_message(attribute, message)
return message if attribute == :base
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(:"errors.formats.attributes.#{attribute}", {
+ :default => [:"errors.format","%{attribute} %{message}"],
@rafaelfranca Owner

:default => [:"errors.format", "%{attribute} %{message}"],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@josevalim
Owner

Thanks. This looks fine to me. Although we need to update the documentation (or properly document Errors#full_messages if it doesn't mention errors.formats yet).

@rafaelfranca

Thank you.

Seems good. I made a code convention comment.

Also we will need a CHANGELOG entry and update the documentation for this.

Unfortunately we don't merge feature addition to 3-2-stable. This branch is only fo bug fixes now.

@jacobo

Thanks for the prompt reply!

Should we submit a CHANGELOG change as part of this pull request?

By documentation do you mean just the bit of comments above def full_message or is there somewhere better?

@rafaelfranca

Yes, CHANGELOG should be added as part of this pull request.

I would add the documentation in the comments above the full_message definition. Something like what is above the def generate_message in the same file.

@sidonath sidonath commented on the diff
activemodel/lib/active_model/errors.rb
@@ -355,8 +355,8 @@ def full_message(attribute, message)
return message if attribute == :base
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(:"errors.formats.attributes.#{attribute}", {

Shouldn't it really be looking for the string under :"#{@base.class.i18n_scope}.errors" (activerecord/activemodel) namespace first (just like it is done for validation error messages)?

Additionally, this doesn't allow us to set different message formats for fields with the same name in different models. Should it support that? Maybe this syntax would be additional fallback? Basically we could have defaults array that's similar to the one constructed in ActiveModel::Errors#generate_message.

Or maybe I should do another pull request to add these things :)

Thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@frodsan

any news on this?

@bai
bai commented

Any updates on this one?

@steveklabnik
Collaborator

@jacobo @ey-pairing it's been... 5 months since we've heard from you. If you're interested in keeping up with this pull request, please let me know, but I'm giving this a close as stale . Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 16, 2012
  1. @ryansouza

    i18n error messages: custom formats by attribute

    ryansouza authored
    First place lookup is now: errors.formats.attributes.#{attribute}
    
    Example use case:
    
        errors:
          attributes:
            last_name:
              blank: you must provide your full name
          formats:
            attributes:
              last_name: "%{message}"
    
    (same fallbacks as before)
This page is out of date. Refresh to see the latest.
View
4 activemodel/lib/active_model/errors.rb
@@ -355,8 +355,8 @@ def full_message(attribute, message)
return message if attribute == :base
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(:"errors.formats.attributes.#{attribute}", {

Shouldn't it really be looking for the string under :"#{@base.class.i18n_scope}.errors" (activerecord/activemodel) namespace first (just like it is done for validation error messages)?

Additionally, this doesn't allow us to set different message formats for fields with the same name in different models. Should it support that? Maybe this syntax would be additional fallback? Basically we could have defaults array that's similar to the one constructed in ActiveModel::Errors#generate_message.

Or maybe I should do another pull request to add these things :)

Thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ :default => [:"errors.format","%{attribute} %{message}"],
@rafaelfranca Owner

:default => [:"errors.format", "%{attribute} %{message}"],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
:attribute => attr_name,
:message => message
})
View
6 activemodel/test/cases/validations/i18n_validation_test.rb
@@ -61,6 +61,12 @@ def test_errors_full_messages_uses_format
assert_equal ["Field Name empty"], @person.errors.full_messages
end
+ def test_errors_full_messages_looks_up_custom_attribute_format
+ I18n.backend.store_translations('en', :errors => {:formats => {:attributes => {:name => "Name is: %{message}"}}})
+ @person.errors.add('name', 'empty')
+ assert_equal ["Name is: empty"], @person.errors.full_messages
+ end
+
# ActiveModel::Validations
# A set of common cases for ActiveModel::Validations message generation that
Something went wrong with that request. Please try again.