[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Validation

Cyrus Dev

12/16/2008 11:47:00 AM

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
--
Posted via http://www.ruby-....

3 Answers

dusty

12/16/2008 2:58:00 PM

0

On Dec 16, 6:47 am, Cyrus Dev <cyrus_...@hotmail.com> wrote:
> 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
> --
> Posted viahttp://www.ruby-....

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)

Cyrus Dev

12/17/2008 4:09:00 AM

0

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-....
>
> 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)

--
Posted via http://www.ruby-....

dusty

12/17/2008 6:38:00 AM

0

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...

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_ra...