Forum: Ruby Validation

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7694e527192e45b73b95f2ca41034463?d=identicon&s=25 Cyrus Dev (cyrusdev)
on 2008-12-16 12:54
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
2e8848a777f7165f80da876ab1e6e7e5?d=identicon&s=25 dusty (Guest)
on 2008-12-16 16:00
(Received via mailing list)
On Dec 16, 6:47 am, Cyrus Dev <cyrus_...@hotmail.com> 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)
7694e527192e45b73b95f2ca41034463?d=identicon&s=25 Cyrus Dev (cyrusdev)
on 2008-12-17 05:16
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 Dev <cyrus_...@hotmail.com> 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)
2e8848a777f7165f80da876ab1e6e7e5?d=identicon&s=25 dusty (Guest)
on 2008-12-17 07:41
(Received via mailing list)
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 %>
<p><%= f.text_field :name, :size => 50 %></p>
<br />

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_...
This topic is locked and can not be replied to.