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?