Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

All possible model validation errors

I have got a form with a bunch of fields and model validations.

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

I need it to write 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

like image 356
fl00r Avatar asked Jun 03 '11 09:06

fl00r


2 Answers

Basically what Pablo says, except that the page on the rails docs doesn't show how to override the messages for a particular model and field. here's an example from one of my apps:

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've also changed the basic format for error messages, as sometimes i didn't want the field name shoved at the start of the message. So, i changed

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

to

errors:
  format: "{{message}}"    

Which is why i then specify {{attribute}} in my subsequent errors, to put it back in in most but not all cases.

Note also that i'm using the old syntax of {{var}} rather than %{var}. The same principles apply though.

like image 178
Max Williams Avatar answered Oct 13 '22 09:10

Max Williams


The latest rails translations are at: rails-i18n.

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

Also in your application under config/locales/en.yml is the default.

like image 39
Pablo Castellazzi Avatar answered Oct 13 '22 09:10

Pablo Castellazzi