# Display validation errors individually

**URL:** <https://www.ruby-forum.com/t/display-validation-errors-individually/103480>\
**Category:** Rails\
**Created:** [May 6, 2007, 8:21pm UTC](https://www.ruby-forum.com/t/display-validation-errors-individually/103480 "2007-05-06T20:21:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [May 6, 2007, 8:21pm UTC](https://www.ruby-forum.com/t/display-validation-errors-individually/103480/1 "2007-05-06T20:21:58Z")

</div>

Hello,

I hope someone can help me with a problem.

Given the following code:

class User \< ActiveRecord::Base  
validates\_uniqueness\_of :username, :message =\> “Username is taken”  
validates\_presence\_of :username  
validates\_presence\_of :password  
end

\<%= error\_messages\_for ‘user’ %\>  
\<%= form\_tag %\>

| Username: | \<%= text\_field("user", "username") %\> | I WANT TO DISPLAY USERNAME ERRORS HERE |
| Password: | \<%= password\_field("user", "password") %\> | I WANT TO DISPLAY PASSWORD ERRORS HERE |
| | |

\<%= end\_form\_tag %\>

I don’t want to display all of the errors in one block using \<%=  
error\_messages\_for ‘user’ %\>

How can I display error messages for specific form fields individually  
as indicated in the HTML above?

Thank you so much for your help.

Chris

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [May 6, 2007, 10:57pm UTC](https://www.ruby-forum.com/t/display-validation-errors-individually/103480/2 "2007-05-06T22:57:22Z")

</div>

On May 6, 2007, at 8:21 PM, Chris wrote:

> Username: \<%= text\_field("user", "username") %\> I WANT TO DISPLAY USERNAME ERRORS HERE
> 
> The starting point is @user.errors.on(:username).
> 
> – fxn

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [May 7, 2007, 8:56pm UTC](https://www.ruby-forum.com/t/display-validation-errors-individually/103480/3 "2007-05-07T20:56:15Z")

</div>

Thanks for your help. I have now solved the problem.

For anyone else experiencing the same problem, here is the code:

\<%= form\_tag :action =\> ‘do\_something’ %\>

| Username: | \<%= text\_field("user", "username") %\> | \<%= error\_message\_on("user", "username") %\> |
| Password: | \<%= password\_field("user", "password") %\> | \<%= error\_message\_on("user", "password") %\> |
| | |

\<%= end\_form\_tag %\>

class MyController \< ApplicationController

def do\_something  
@user = User.new(@params[‘user’])  
if @user.save  
# User was saved  
else  
# User was not saved so redisplay the form  
end  
end

end
