0

For the most part, I find that Rails default error messages for most of the built-in validators work fine for me. However, I also have a few validations on some models where the default "{{attribute}} {{message}}" full_message format just doesn't make any semantic sense. I know that I can display the messages instead of the full_messages, but that would mean I have to add in my own messages to every built-in validation, because otherwise the attribute name would be chopped off.

Is there a way to use the default full_message format, but to directly override the full_message for just a handful of validations to use a different format or a particular string? Is there a natural way to mix the default validation messages with a few custom ones that don't start with the attribute name?

James Milani
  • 1,921
  • 2
  • 16
  • 26
xanderflood
  • 826
  • 2
  • 12
  • 22
  • 1
    I think you're going to want to implement what's described in this answer: https://stackoverflow.com/a/2859275/3741320 – James Milani Aug 21 '17 at 23:27
  • 1
    The `add_to_base` method mentioned in there was exactly what I needed, except that it was deprecated and finally removed in 4.2, and I'm using Rails 5. The new syntax is apparently `errors(:base, "message")`. In other words, if you use :base as the attribute symbol, Rails will *not* add the attribute name to your message. – xanderflood Aug 21 '17 at 23:55

2 Answers2

0

You can add custom messages to config/locales/en.yml

hashrocket
  • 2,210
  • 1
  • 16
  • 25
  • This doesn't seem to solve my problem. I can store my messages there, but I18n still automatically prepends the attribute name. – xanderflood Aug 21 '17 at 23:45
0

In Rails 4, the errors.add_to_base("Something is terribly wrong!") would add an error to the base model, instead of an attribute, and in this case full_message returns the same thing as message with no prepended attribute name.

This was removed in rails 4.2, and the new way to do it is with errors.add(:base, "Something is terribly wrong!").

xanderflood
  • 826
  • 2
  • 12
  • 22