-
Notifications
You must be signed in to change notification settings - Fork 22k
Activemodal::Errors added methods error_message_format and i18n_priority_format #14260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
.gitignore
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💥 You shouldn't add these.
I'm not sure about this, if I'm not wrong something like that has been already proposed. There's a performance consideration to take into account that this adds tons of extra I18n lookups for a single attribute error. Can you give more context on what's your use case for this? Thanks. |
#7369 is another take on this one. |
Example use case:
User received error message: Name can't be blank. Phone can't be blank. Current version "full_message" method not available this case.
User received error message: Name can't be blank. Enter your phone. |
@carlosantoniodasilva
|
Any update on this? :( |
@TemaMix can you provide the code you used to benchmark? |
I've been looking at this as well, and agree the concept of being able to modify the full message format with more granularity is needed. I think there are a few things to discuss here, but let's start with: Why does this same concept exist in 2.3 and not in 3.0? rails 2.3.4 with support for modifying full_messages format per attribute: rails 3.0.0 with support for only a single full_messages format: I'm not exactly sure why this is not in 3.x and later. My hunch is that it wasn't intentional, and due to the timing of 3.x development the change was implemented on 2.x and failed to be ported to 3.x. If someone else knows for sure, that would be a helpful place to start. To your point @carlosantoniodasilva and @rafaelfranca, the lookup performance consideration seems to be the next question. Per that, here's a benchmark: https://gist.github.com/eprothro/c6712e1644abd3379d06 It's what you would expect, N lookups takes N times longer. However, we're already doing N lookups in The more I've thought about it, the more I think, if I were being told about this interface fresh, I would expect to have control over full_message just like I do the error message. As used by
If
Allowing the example of a one-off full message without the prepended attribute:
Maybe let's start by discussing this design before getting into the implementation implications? |
Was this solved? |
👍 A lot of questions on StackOverflow related to this one. Let's fix this. |
This may be helpful http://adamhooper.com/eng/articles/5 |
I'd like to be able to do this so I don't have to use the custom_error_message gem and prepend "^" to every message that the customer doesn't want to start with the attribute name. |
/railties/doc | ||
/railties/tmp | ||
/guides/output | ||
/guides/output |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is diff noise, it would be nice if this was reverted to minimize the noise.
def self.lookup_ancestors | ||
[self] | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is diff noise, it would be nice if this was reverted to minimize the noise.
person.errors.expects(:generate_message).with(:name, :blank, { message: 'custom' }) | ||
person.errors.add_on_blank :name, message: 'custom' | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is diff noise, it would be nice if this was reverted to minimize the noise.
Closing in favor of #32956. |
error_message_format - returns an I18n key of an error message taking into consideration the format of an error message. If none found the default value is being returned (errors.format).
Formats are first looked up in "activerecord.errors.models.MODEL.attributes.ATTRIBUTE.format", if it's not there, it's looked up in "activerecord.errors.models.MODEL.format" and if that is not there also, it returns default format (e.g. "errors.format").
i18n_priority_format - returns array that defined format priority key for i18n scope an error message. Last array element have highest priority.
person.errors.i18n_priority_format # => [:"errors.format"]
person.errors.i18n_priority_format(:name)
# => [:"errors.format",
# :"activemodel.errors.models.person.format",
# :"activemodel.errors.models.person.attributes.name.format"]