# How are error\_messages\_for sorted?

**URL:** <https://www.ruby-forum.com/t/how-are-error-messages-for-sorted/88354>\
**Category:** Rails\
**Created:** [March 9, 2007, 11:01pm UTC](https://www.ruby-forum.com/t/how-are-error-messages-for-sorted/88354 "2007-03-09T23:01:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 9, 2007, 11:01pm UTC](https://www.ruby-forum.com/t/how-are-error-messages-for-sorted/88354/1 "2007-03-09T23:01:22Z")

</div>

This problem is driving me crazy…

I’ve implemented the Ruby Cookbook method of user accounts. I’ve got  
the following code in my user model…

validates\_presence\_of :username, :message =\> “can’t be blank”  
validates\_uniqueness\_of :username, :message =\> “is already taken!”  
validates\_length\_of :username, :maximum =\> 21, :message =\> “must be  
less than 21 characters”  
validates\_confirmation\_of :password,  
:if =\> lambda { |user| user.new\_record? or not  
user.password.blank? }  
validates\_length\_of :password, :within =\> 6…20,  
:if =\> lambda { |user| user.new\_record? or not  
user.password.blank? }validates\_presence\_of :gender, :message =\> "must  
be selected"validates\_acceptance\_of :tos, :message =\> “must be  
accepted by checking the box”  
validates\_format\_of :email, :with =\> /^([^@\s]+)@((?:[-a-z0-9]+.)+[a-  
z]{2,})$/i

And the following in my new user add page…

\<%= error\_messages\_for ‘user’ %\>  
\<%= form\_tag :action =\> ‘add’ %\>  
Desired Username: \<%= text\_field “user”, “username” %\>  
  
Valid Email Address: \<%= text\_field “user”, “email” %\>  
  
Password (6 to 20 characters): \<%= password\_field “user”, “password”  
%\>  
  
Verify Password: \<%= password\_field “user”, “password\_confirmation”  
%\>  
  
Please Choose a Gender  
\<%= radio\_button(“user”, “gender”, “m”)  
%\>Male  
\<%= radio\_button(“user”,  
“gender”, “f”) %\>Female

\<%=check\_box("user", "tos") %\> I am at least 18 years old. I have read and agree to the \<%= link\_to "Terms Of Service", :controller =\> "about", :action =\> "tos" %\>.

\<%= image\_submit\_tag("signup.gif")%\> \<%= end\_form\_tag %\>

Here is the problem. If someone submits a form with errors, the error  
messages are presented in a different order than I would expect. For  
example, if someone submits a blank form, they get back:

There were problems with the following fields:

Tos must be accepted by checking the box  
Username can’t be blank  
Gender must be selected  
Password is too short (minimum is 6 characters)  
Email is invalid

This doesn’t follow the order in the model, the order of the form, or  
any logical order I can figure out. I really want the errors  
displayed in the same order that they appear on the form. Any ideas?  
I did search the group and learned I can step through the errors  
manually, using error\_message\_on :user, :username  
error\_message\_on :user, :email

But this doesn’t seem very agile… if I add a field to the form  
later, then I’ll need to update the error\_message\_on section too. Why  
doesn’t rails display the errors in the same order as the validations  
in the model?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 10, 2007, 12:40pm UTC](https://www.ruby-forum.com/t/how-are-error-messages-for-sorted/88354/2 "2007-03-10T12:40:31Z")

</div>

On 9 Mar 2007, at 22:00, removed\_email\_address@domain.invalid wrote:  
[snip]

> This doesn’t follow the order in the model, the order of the form, or  
> any logical order I can figure out.

That’s because the errors are stored in a hash and “the order in  
which keys and/or values are returned by the various iterators over  
hash contents may seem arbitrary and will generally not be in  
insertion order.” (Quotation from the Pickaxe book.)

> I really want the errors  
> displayed in the same order that they appear on the form. Any ideas?  
> I did search the group and learned I can step through the errors  
> manually, using error\_message\_on :user, :username  
> error\_message\_on :user, :email

I agree – it would indeed be simpler for the user if the errors  
displayed in the same order as the relevant form elements.

> But this doesn’t seem very agile… if I add a field to the form  
> later, then I’ll need to update the error\_message\_on section too. Why  
> doesn’t rails display the errors in the same order as the validations  
> in the model?

You seem to be suggesting a convention where the order of validation  
declarations in the model matches the order of form elements in your  
view. I’m not sure that’s optimal…what happens if you have two  
views of the same model with fields in a different order?

This post and plugin may help you out (though as I write this I can  
only view the page via Google’s cache):

[http://www.railtie.net/articles/2006/01/26/enhancing\_rails\_errors](http://www.railtie.net/articles/2006/01/26/enhancing_rails_errors)

Regards,  
Andy S.
