Messages & locales
Validation messages are templates. When a validator fails it renders a template into a string, substituting tokens drawn from the record and the validator options. The template is chosen in this order:
- An explicit
messageoption on the validator declaration. - A template registered for the current locale (see Locales).
- The built-in default for that error type.
Message tokens
Every template is interpolated with the tokens below. An undefined token is left untouched, so a template that does not mention a token is unaffected.
| Token | Substituted with |
|---|---|
{model} |
The model's class name (e.g. User) |
{attribute} |
The attribute name, or the as label when one is set |
{value} |
The bound value for the failing validator (the length, the threshold) |
{count} |
The numeric bound for length, numericality, and comparison validators |
| any option | Any extra option passed to errors.add (for example {count}) |
1 2 3 4 5 6 7 8 9 10 11 | |
Locales
ORM::ActiveRecord::Support::I18n holds per-locale message templates and the
currently active locale. Register templates with store, then switch the active
locale with set-locale.
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
Error types use the same hyphenated names the validators record: blank,
too-long, too-short, wrong-length, greater-than, less-than, taken,
accepted, confirmation, inclusion, exclusion, invalid, and the rest
listed in The errors collection.
Lookup order
For a given model, attribute, and error type, the most specific registered template wins. The keys are searched in this order:
activerecord.errors.models.MODEL.attributes.ATTRIBUTE.TYPEactiverecord.errors.models.MODEL.TYPEactiverecord.errors.messages.TYPEerrors.attributes.ATTRIBUTE.TYPEerrors.messages.TYPE
MODEL is the lower-cased class name and ATTRIBUTE is the column name. A
model-and-attribute override lets one attribute read differently from the rest:
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
When no template is registered for the active locale, the lookup retries under
default-locale (also en by default), then falls back to the built-in
defaults. Nothing breaks if a locale is partially translated.
Switching locale for a block
with-locale runs a block under a temporary locale and restores the previous
one afterwards, even if the block dies.
1 2 3 4 5 6 7 | |
Managing the registry
1 2 3 4 5 6 | |
errors.add resolves through the same locale store, so messages added by hand or
from a custom validator are localised too:
1 2 3 4 5 | |