All possible model validation errors

I have a form with a bunch of fields and model validation.

How can I return all possible validation errors that may be raised?

I need them to record locales for all of them.

I want to get a list like this:

password blank password too_short password confirmation login blank login invalid email blank email too_short email invalid 

etc.

+7
source share
2 answers

Basically what Pablo says, except that the docs page on rails does not show how to redefine messages for a specific model and field. here is an example from one of my applications:

 activerecord: errors: full_messages: format: "{{message}}" #define standard error messages, which we can overide on per model/per attribute basis further down messages: inclusion: "{{attribute}} is not included in the list" exclusion: "{{attribute}} is reserved" invalid: "{{attribute}} is invalid" confirmation: "{{attribute}} doesn't match confirmation" accepted: "{{attribute}} must be accepted" empty: "{{attribute}} can't be empty" blank: "{{attribute}} can't be blank" too_long: "{{attribute}} is too long (maximum is {{count}} characters)" too_short: "{{attribute}} is too short (minimum is {{count}} characters)" wrong_length: "{{attribute}} is the wrong length (should be {{count}} characters)" taken: "{{attribute}} has already been taken" not_a_number: "{{attribute}} is not a number" greater_than: "{{attribute}} must be greater than {{count}}" greater_than_or_equal_to: "{{attribute}} must be greater than or equal to {{count}}" equal_to: "{{attribute}} must be equal to {{count}}" less_than: "{{attribute}} must be less than {{count}}" less_than_or_equal_to: "{{attribute}} must be less than or equal to {{count}}" odd: "{{attribute}} must be odd" even: "{{attribute}} must be even" record_invalid: "Validation failed: {{errors}}" models: quiz: blank: "{{attribute}} can not be blank" user_session: blank: "{{attribute}} can not be blank" attributes: login: invalid: "Please enter your user name" password: invalid: "Please note that passwords are case sensitive" 

I also changed the basic format of error messages, because sometimes I did not want the field name to be loaded at the beginning of the message. So I changed

 errors: format: "{{attribute}} {{message}}" 

to

 errors: format: "{{message}}" 

That is why I then point out {{attribute}} in my subsequent errors to return it in most, but not all cases.

Note also that I'm using the old syntax {{var}} , not %{var} . However, the same principles apply.

+12
source

Recent rail transfers: rails-i18n .

ActiveRecord errors are located under lang: errors and lang: active_record in each .yaml.

Also in your application, config / locales / en.yml uses the default value.

+3
source

All Articles