Rails validations, how to improve them

Here is a discussion of a problem and potential solution for Rails
validations.
Rails has a fundamental problem with validations in that it essentially
requires you to either

  1. Accept the default error messages (which can often lead to ugly and
    unfriendly messages for the user)
  2. Override validation messages at the model level like so

validates_uniqueness_of :field, :message => “You’ve gotta enter
something unique please!”

Probably #1 is good for casual applications but for anything production
worthy you will want to have pretty error messages. Putting validation
messages in the models seems fundamentally wrong because they are a view
level thing (for example you may want two different mesages in different
places depending on the reason for the model failing, or you might want
different languages)

The solution? Well it’s simple…we need to know which validation
failed
in the error array. So right now the errors look like this

“field” => “message”

In order to be able to specify a custom error message properly we need
to know why the field failed so the appropriate structure would look
something like:

@user.errors = {:validates_uniqueness_of => :name, :validates_format_of
=> :email}

This tells us that the uniqueness of name failed and that the format of
the email is wrong. From these tuples we can easily extract the
appropriate error message from an external language file or something
like that. With the current rails core the validation method that is
failing is not given int he errors array on the model so you don’t have
enough information to pull this out.

Now, someone go write a plugin becaues I’m too lazy :-). Actually I may
do this one day but right now the rails core is so different in this
place that it will require extensive surgery to make it store the
failing method, not just the name of the failed field.