Validation

Hi ,

any one know how to order validation messages in Rails

I have model , with following validation like

name, email, password

but when it validates ,messages appear unordered…becuase rails store
validation messages in hash…

so please how to solve this probelm

thanks

On Dec 16, 6:47 am, Cyrus D. [email protected] wrote:

so please how to solve this probelm

thanks

Posted viahttp://www.ruby-forum.com/.

You can ask for specific error messages. I usually do that right
above the field on the form. Say you have an item object with a
location property.

<%= error_message_on :item, :location %>
<%= f.label :location %>
<%= f.text_field :location %>

That will add the location error message above it. Only when there is
an error on the location property, will it show up.

You can also pull specific errors with the on method on the errors
collection. Say you have a user, with an error on the name property.

u = User.new
u.valid?
u.errors.on(:name)

Hi dusty ,
thanks for your reply.

but I want to show error messages like it appears above forum

like

Following fields has problem

Name
email
password

in order they written in model

I have many forms … so is there a way to make generalize… ?

it’s not good to write error message for each field and for each form…

dusty wrote:

On Dec 16, 6:47�am, Cyrus D. [email protected] wrote:

so please how to solve this probelm

thanks

Posted viahttp://www.ruby-forum.com/.

You can ask for specific error messages. I usually do that right
above the field on the form. Say you have an item object with a
location property.

<%= error_message_on :item, :location %>
<%= f.label :location %>
<%= f.text_field :location %>

That will add the location error message above it. Only when there is
an error on the location property, will it show up.

You can also pull specific errors with the on method on the errors
collection. Say you have a user, with an error on the name property.

u = User.new
u.valid?
u.errors.on(:name)

The errors are added to the model in the order they are run in
validations and have no relationship to the order they are in your
form. So, its not an easy fix.

In my opionion, its a better user experience if the messages are next
to the actual error field. That way users don’t have to shift their
attention to the top and then back to each field they need to
correct. They can just move their focus to what needs to get fixed
and see all the helpful information you are providing to them. So, in
case you do decide to do it that way, here is what an actual form
piece looks like in the manual setup that I might use.

<%= error_message_on :user, :name, 'Name ’ %>
<%= f.label :name %>

<%= f.text_field :name, :size => 50 %>


Now, to get to what you’re looking for (not having to add that to each
form field). I did find this post, where they override how rails
makes the highlighted box around the form field to highlight the
error. They put the message there as I was doing, but it looks like
this way will do that automatically for you.

http://snippets.dzone.com/posts/show/3403

Otherwise, you may have to setup an array in the specific order that
you want and then iterate through it, spitting out each error in the
order you want.

I also found this older post that looks to be doing the same thing you
are. Perhaps that can help guide you.

http://i.nfectio.us/articles/2006/01/26/enhancing_rails_errors