Form Validation Ruby on Rails
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 fai开发者_运维问答ls, all the errors are listed above the form....I want the errors to be displayed corresponding to the textboxes.....how do i do that
Put this next to your check-box / text field or other form elements:
<%= @user.errors.on(:attribute_name) %>
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/
精彩评论