I would like to create a custom message that shows to the user whenever they're trying to register with the email address that is already used. I would like the text to say "Provided email address is already used". I'm passing it to my en.yml file as follows:
active_record:
models:
user:
attributes:
email:
taken: "Provided %{attribute} address is already used
The problem is, that this results in automatic interpolation of email attribute at the beginning of the text, so I'm ending up with "Email Provided Email address is already used". Is there a way to:
- Remove automatic attribute interpolation?
- Lowercase attribute name?
CodePudding user response:
- To avoid the attribute prefix, you'll want to set the
format
of the error messages like so:
en:
errors:
format: "%{message}"
header: 'The form entry has errors; please correct the errors below and re-submit.'
messages:
taken: "The provided %{attribute} is already taken; please enter a different %{attribute}."
(It is always good to tell the user what the problem is, but also what the solution is even if it seems obvious. I have provided an error message example with that in mind. I also included a header
as well since it is a good idea to use at the top of the form if you're putting errors next to their respective fields in the form.)
- To make the attribute name lowercase, you'll want to set the attribute name explicitly in your locale file, like so:
en:
activerecord:
attributes:
user:
email: 'e-mail'
This will also likely change how the attribute is displayed in your views, so keep that in mind. I suggest you keep the capitalization as it actually makes the error messages clearer.
Also keep in mind my structure and organization of the locale file is based on Rails 4.2. You might need slightly different organization if you're using a different version of Rails.
And just for fun, here are good examples for most of the built-in validations.
en:
errors:
format: "%{message}"
header: 'The form entry has errors; please correct the errors below and re-submit.'
messages:
accepted: "The %{attribute} was not accepted; please accept the %{attribute}."
blank: "The provided %{attribute} is blank; please enter a non-blank %{attribute}."
confirmation: "The provided %{attribute} does not match the corresponding entry; please re-check this entry against the original."
empty: "The provided %{attribute} is empty; please enter a non-empty %{attribute}."
equal_to: "The provided %{attribute} is incorrect; please enter exactly %{count}."
even: "The provided %{attribute} is odd; please enter an even %{attribute}."
exclusion: "The provided %{attribute} is reserved; please enter a different %{attribute}."
greater_than: "The provided %{attribute} is too small; please provide a different %{attribute} greater than %{count}."
greater_than_or_equal_to: "The provided %{attribute} is too small; please provide a different %{attribute} greater than or equal to %{count}."
inclusion: "The chosen %{attribute} is not available; please choose an available option." # Rails 4
inclusion_of: "The chosen %{attribute} is not available; please choose an available option." # Rails 5
invalid: "The provided %{attribute} is invalid; please enter a valid %{attribute}."
in_between: "The provided %{attribute} is outside of the accepted range; please enter a different %{attribute} within the range of %{min} to %{max}."
less_than: "The provided %{attribute} is too large; please provide a different %{attribute} less than %{count}."
less_than_or_equal_to: "The provided %{attribute} is too large; please provide a different %{attribute} less than or equal to %{count}."
not_a_number: "The provided %{attribute} is not numeric; please enter a numeric %{attribute}."
odd: "The provided %{attribute} is even; please enter an odd %{attribute}."
record_invalid: "The %{attribute} is invalid. %{errors}"
spoofed_media_type: "The provided %{attribute} is invalid (often due to an incorrect file extension); please provide a valid %{attribute}, including an appropriate file extension."
too_long: "The provided %{attribute} contains more than the %{count} available characters; please shorten the entry."
too_short: "The provided %{attribute} contains fewer than the %{count} required characters; please lengthen the entry."
taken: "The provided %{attribute} is already taken; please enter a different %{attribute}."
wrong_length: "The provided %{attribute} contains the wrong amount of characters; please adjust the entry to exactly %{count} characters."