Where to find Rails’ default validation error messages

Thoughts from the team
By    | February 9, 2012 | Tips & Tricks,

I’ve been playing around with simple_form and I18n in Rails a LOT in a recent project. I’ve been customising form labels and validation messages using the internationalisation features – it works really, really well.  There is, however, one small problem.  When you start, you are presented with an almost blank YAML file and some very flakey documentation at the bottom of the simple_form readme.  How do you know how to structure that YAML file?  What attribute names should you use?

One of the best resources I’ve found so far are the defaults files in the Rails source code.

https://github.com/rails/rails/blob/master/activemodel/lib/active_model/locale/en.yml
https://github.com/rails/rails/blob/master/activerecord/lib/active_record/locale/en.yml

That gives you a full list of the attribute names to use for different validators.  For example, if you’re using a validates_format_of validator, you can quickly see that you need to use the invalid YAML attribute.  Good luck guessing that one!

The looking at the source trick can also be very useful for gems.  I’ve spent ages Googling for the correct YAML attribute to use for the validates_email_format_of gem with absolutely no luck whatsoever.  30 seconds on Github and I find the source file which tells me the attribute for an invalid email address is invalid_email_address.

More like this