rails custom validation messages i18n

I did not find on the net how to override the “{attribute} {message}” format of validation error messages. It is no use in my language. Sometimes I need a different order or specific message. The way I managed to do it using rails 2.3.4 is:


  1. use validation message in model, for example:
  validates_presence_of     :login, :message => :login_blank
  1. Define the message in your locale in the yml, (en.yml, pl.yml or whatever yours is) under the tree (this example is for “pl” locale):
pl:
(...)
  activerecord:
    (...)
    errors:
      (...)
      full_messages:
        login_blank: "My custom message hahaha!"
(...)

I found this by looking at: RAILS_HOME/activerecord/lib/active_record/validations.rb and the comment to generate_full_message method

Posted by wojtek Mon, 16 Nov 2009 18:23:00 GMT




Comments

Leave a response

  1. Simon 4 months later:

    Thanks, this helped!

Leave a comment