3

My understanding is that I can customize error messages in the localization files. I'm currently trying to customize the error message for an invalid username field on my User model:

User.rb

class User < ActiveRecord::Base
    validates :username,    length: { within: 3..25 },
                            format: /\A(?=.*[a-z])[a-z\d]+\Z/i,
                            uniqueness: true

en.yml (localization file)

en:
  activerecord:
    errors:
      models:
        user:
          attributes:
            username:
              format: "CUSTOM MESSAGE!"

The error message I receive, however, is the default "is invalid" message (or "Username is invalid" if I get the full_messages version).

1 Answer 1

2

Have a look here. There is no format key in the errors/messages. You need to override the invalid key.

en:
  activerecord:
    errors:
      models:
        user:
          attributes:
            username:
              invalid: "CUSTOM MESSAGE!"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.