Keep Models out of Your Views' Business! (Part 2)

Last week, I wrote about using Rails’ I18N facilities to break dependencies between your models and views, by changing how model and attribute names are displayed. But there’s another place where the implementation of models sometimes peeks through into views: validation error messages.

Depending on how you use them, Rails’ error message helpers may insert attribute names into messages, and the changes from last week will take care of that automatically. But it goes farther than that. Often, validation error messages express things in a way that is not appropriate for end users. The usual solution is to override the default messages with `:message => '...'` options on the validation declarations. But again, that means the model is actively involved in presentation issues, and we’d like to avoid that if possible.

Whole plugins have been written to try to solve this problem, but Rails internationalization support has made it much easier. When validation error messages have to refer to the name of a model class or attribute, they will use the locale definitions we’ve already supplied. And it turns out that they look in the locale for error message text, too.

Overriding Validation Error Messages

Last week, we used a typical Rails example: a blogging app with a model called `Post` with attributes `title` and `body`. (And we used I18N to relabel `Post` as `Article`, and `title` as `headline`.) Let’s continue with that example, assuming this declaration in `Post`:

  validates_presence_of :title

The default error message (with the re-labeling we’ve already done) is “Headline can’t be blank.” How would we change that to something friendlier, like “Headline should not be empty; please supply one”?

Let’s add a little more to `en.yml`—an “errors” section that includes customized validation error messages:

  en:
    activerecord:
      # models and attributes sections carried over from
      # previous article
      models:
        post: "Article"
      attributes:
        post:
          title: "Headline"
      # errors section is new
      errors:
        models:
          post:
            attributes:
              title:
                blank: "should not be empty; please supply one"

We’re now several levels deep in this YAML structure, but it should be fairly easy to follow. Under `activerecord.errors` there’s a section for `models`, within which we find our model (`post`). Of its `attributes` we’ve supplied a custom error message for the `title` attribute; in particular, we’ve customized the `blank` message.

With that change in place, the error messages will read as we’d like them to, with no change to the model or the views.

Note that we restricted this change to just the `title` attribute of `Post`. If we wanted to change all “can’t be blank” messages for all attributes of `Post`, we could do that as well:

  en:
  activerecord:
    errors:
      models:
        post:
          blank: "should not be empty; please supply one"

Finally, if we wanted to make the change across all the models, here’s how:

en:
  activerecord:
    messages:
      blank: "should not be empty; please supply one"

Which Messages Can I Override?

You can see the current default messages in the source code for the activerecord gem; just consult the file `lib/active_record/locale/en.yml`. But here’s the entire list, mapped to the validation methods that use them. (Some validations use different messages depending on the circumstances.)

validates_acceptance_of
`:accepted` (“must be accepted”)
validates_associated
`:invalid` (“is invalid”)
validates_confirmation_of
`:confirmation` (“doesn’t match confirmation”)
validates_exclusion_of
`:exclusion` (“is reserved”)
validates_format_of
`:invalid` (“is invalid”)
validates_inclusion_of
`:inclusion`(“is not included in the list”)
validates_length_of
`:too_short` (“is too short (minimum is 8 characters)”)
`:too_long` (“is too long (maximum is 8 characters)”)
validates_length_of (with :is option)
`:wrong_length` (“is the wrong length (should be 8 characters)”)
validates_numericality_of
`:not_a_number` (“is not a number”)
validates_numericality_of (with :odd option)
`:odd` (“must be odd”)
validates_numericality_of (with :even option)
`:even` (“must be even”)
validates_numericality_of (with :greater_than option)
`:greater_than` (“must be greater than 8”)
validates_numericality_of (with :greater_than_or_equal_to option)
`:greater_than_or_equal_to` (“must be greater than or equal to 8”)
validates_numericality_of (with :equal_to option)
`:equal_to` (“must be equal to 8”)
validates_numericality_of (with :less_than option)
`:less_than` (“must be less than 8”)
validates_numericality_of (with :less_than_or_equal_to option)
`:less_than_or_equal_to` (“must be less than or equal to 8”)
validates_presence_of
`:blank` (“can’t be blank”)
validates_uniqueness_of
`:taken` (“has already been taken”)

Interpolating values

You probably noticed that some of the default messages contain the string `8`. Validations that use some threshold value pass that value into the I18n library as the option `count`, and I18n interpolates the count value into the message string.

In addition to `count`, all of the validations supply three other values that can be interpolated: `model` and `attribute` (the model and attribute names, already humanized as discussed in part 1) and `value` (the erronenous value of the attribute). You can write your error messages to make use of any of those values, using the same double-curly-brace syntax.

Writing custom validations

If you write your own validation methods, whether just for your project or in a plugin, you should make use of the same mechanisms.

When a validation detects an error, it reports that error by calling the `#add` method on the `errors` object. Here’s an example (taken from `validates_inclusion_of`, and slightly modified to make more sense out of context):

  record.errors.add(attr_name, :inclusion, 
                               :value => value, 
                               :default => options[:message]) 

The first parameter is the name of the attribute being validated. Second is the symbol that’s used to look up the default message text from the I18n message repositories. Finally there’s an options hash, which includes the current attribute value (for possible interpolation into messages) and any new message text that may have been supplied directly on the call to `validates_inclusion_of`.

(That last bit is somewhat confusing; the option to `add` is called `default`, but it’s actually a specific override. That bit of weirdness is there to maintain some internal compatibility for the benefit of plugins that were written for older versions of Rails. I wouldn’t be surprised to see that cleaned up in Rails 3, although for now it still works the same way.)

All you need to do in your custom validation is to use the `#add` method in the same way, and supply default error message text in a message repository, under the `en.activerecord.messages` key. You can do it directly in `config/locales/en.yml`, like this:

  en:
    activerecord:
      messages:
        bozo: "can't be ridiculous"

Plugins and gems can supply their own YAML file. They just need to tell Rails about it by pushing the file path onto `I18n.load_path`.

Conclusion

As I said last week, the fact that Rails uses model and attribute names in views isn’t a bad thing; it’s a useful convention that helps developers move quickly. The test of a framework like Rails is how easy it is to break those links when you need to. Rails’ internationalization support makes it easy to solve this particular problem, with the added benefit that it makes your application easier to localize, should you need to go down that path.

I’ve completely stopped overriding validation error messages directly in the model code. You should, too!

Get In Touch