# Rails validations, how to improve them

**URL:** <https://www.ruby-forum.com/t/rails-validations-how-to-improve-them/92518>\
**Category:** Rails\
**Created:** [April 20, 2007, 6:49am UTC](https://www.ruby-forum.com/t/rails-validations-how-to-improve-them/92518 "2007-04-20T06:49:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![skwp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@skwp](https://www.ruby-forum.com/u/skwp)\
**Post date:** [April 20, 2007, 6:49am UTC](https://www.ruby-forum.com/t/rails-validations-how-to-improve-them/92518/1 "2007-04-20T06:49:07Z")

</div>

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.
