views:

36

answers:

2

Hi All, I have done a validation check in my form to check for email,username and firstname...here is the code

class User < ActiveRecord::Base
    validates_presence_of :email, :firstname, :username


    validates_format_of :email,
                    :with => /[-!#$&'*+\/=?`{|}~.\w]+@[a-zA-Z0-9]([-a-zA-Z0-9]*[a-zA-Z0-9])*(\.[a-zA-Z0-9]([-a-zA-Z0-9]*[a-zA-Z0-9])*)+$/,
                    :message => ' appears to be invalid'
end

And in my view part is looking like this

<%= form_for(@user) do |f| %>
  <% if @user.errors.any? %>
    <div id="error_explanation">
       <ul>
       <% @user.errors.full_messages.each do |msg| %>
         <li>
          <%= msg %>
   </li>
       <% end %>
      </ul>
    </div>

The issue is wenever validation fails, all the errors are listed above the form....I want the errors to be displayed corresponding to the textboxes.....how do i do that

A: 

model validation is really like that. if you want inline form validation, you can go checkout javascript form validation. My favorite is jquery's ketchup plugin:

http://demos.usejquery.com/ketchup-plugin/

corroded
+2  A: 

Put this next to your check-box / text field or other form elements:

<%= @user.errors.on(:attribute_name) %>
Swanand
thanks dude....also i have one more doubt......if i prefer to submit my form without entering any value in email field, am getting 2 errors at a time* Email can't be blank* Email appears to be invalid. How do i display only 1st comment
Sanjai Palliyil