Where can I find a list of Rails error messages keys?
Asked Answered
H

4

9

I'm trying to customize my error messages by modifying en.yml.

en:
  errors:
    messages:
      blank: "This is a required field."

And now, every empty field that has the validates presence: true validator shows this new message.

I want to find a list of messages types to modify. For example, how can I customize the numericality validator message? Or the greater_than validator?

Any suggestions where to find this?

Hyperpituitarism answered 28/7, 2013 at 3:7 Comment(0)
H
15

Here is a list of the messages you can customize in the en.yml file:

validates_acceptance_of
`:accepted` (“must be accepted”)
validates_associated
`:invalid` (“is invalid”)
validates_confirmation_of
`:confirmation` (“doesn’t match confirmation”)
validates_exclusion_of
`:exclusion` (“is reserved”)
validates_format_of
`:invalid` (“is invalid”)
validates_inclusion_of
`:inclusion`(“is not included in the list”)
validates_length_of
`:too_short` (“is too short (minimum is {{count}} characters)”)
`:too_long` (“is too long (maximum is {{count}} characters)”)
validates_length_of (with :is option)
`:wrong_length` (“is the wrong length (should be {{count}} characters)”)
validates_numericality_of
`:not_a_number` (“is not a number”)
validates_numericality_of (with :odd option)
`:odd` (“must be odd”)
validates_numericality_of (with :even option)
`:even` (“must be even”)
validates_numericality_of (with :greater_than option)
`:greater_than` (“must be greater than {{count}}”)
validates_numericality_of (with :greater_than_or_equal_to option)
`:greater_than_or_equal_to` (“must be greater than or equal to {{count}}”)
validates_numericality_of (with :equal_to option)
`:equal_to` (“must be equal to {{count}}”)
validates_numericality_of (with :less_than option)
`:less_than` (“must be less than {{count}}”)
validates_numericality_of (with :less_than_or_equal_to option)
`:less_than_or_equal_to` (“must be less than or equal to {{count}}”)
validates_presence_of
`:blank` (“can’t be blank”)
validates_uniqueness_of
`:taken` (“has already been taken”)
Hyperpituitarism answered 28/7, 2013 at 3:22 Comment(1)
"Any suggestions where to find this?"Procter
M
8

Further to Serg's answer, you can find the list of keys for built-in validators here:

http://guides.rubyonrails.org/i18n.html#error-message-interpolation

Ment answered 23/11, 2013 at 3:34 Comment(0)
T
0

It finds error message in a chain of namespaces. Consider a User model with a validation for the name attribute like this:

class User < ActiveRecord::Base
  validates :name, presence: true
end

The key for the error message in this case is :blank. Active Record will look up this key in the namespaces:

activerecord.errors.models.[model_name].attributes.[attribute_name]
activerecord.errors.models.[model_name]
activerecord.errors.messages
errors.attributes.[attribute_name]
errors.messages

Thus, in our example it will try the following keys in this order and return the first result:

activerecord.errors.models.user.attributes.name.blank
activerecord.errors.models.user.blank
activerecord.errors.messages.blank
errors.attributes.name.blank
errors.messages.blank

Hope it will help. Thanks

Tellurate answered 28/7, 2013 at 4:26 Comment(0)
P
0

To add on to @MikeL answer, which only shows the keys, the full list with the keys and interpolated messages can be found here at rails very own github repository.

Pavilion answered 9/11, 2019 at 2:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.