#211 Validations in Rails 3
Rails 3 offers several new additions to validations. Here learn how to make a custom error_messages partial, reflect on validations, and clean up complex validations in a model.
- Download:
- source codeProject Files in Zip (93.4 KB)
- mp4Full Size H.264 Video (15.9 MB)
- m4vSmaller H.264 Video (10.2 MB)
- webmFull Size VP8 Video (25.7 MB)
- ogvFull Size Theora Video (20.5 MB)
Resources
bash
gem install rails --pre
gem cleanup
rails store
cd store
rails g scaffold user name:string email:string
rake db:migrate
gem install rails --pre gem cleanup rails store cd store rails g scaffold user name:string email:string rake db:migrate
users/_form.html.erb
<%= form_for(@user) do |f| %>
<%= render "shared/error_messages", :target => @user %>
<div class="field">
<%= f.label :name %><%= mark_required(@user, :name) %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %><%= mark_required(@user, :email) %><br />
<%= f.text_field :email %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= form_for(@user) do |f| %> <%= render "shared/error_messages", :target => @user %> <div class="field"> <%= f.label :name %><%= mark_required(@user, :name) %><br /> <%= f.text_field :name %> </div> <div class="field"> <%= f.label :email %><%= mark_required(@user, :email) %><br /> <%= f.text_field :email %> </div> <div class="actions"> <%= f.submit %> </div> <% end %>
shared/_error_messages.html.erb
<% if target.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(target.errors.count, "error") %> prohibited this record from being saved:</h2>
<ul>
<% target.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<% if target.errors.any? %> <div id="errorExplanation"> <h2><%= pluralize(target.errors.count, "error") %> prohibited this record from being saved:</h2> <ul> <% target.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %>
application_helper.rb
def mark_required(object, attribute)
"*" if object.class.validators_on(attribute).map(&:class).include? ActiveModel::Validations::PresenceValidator
end
def mark_required(object, attribute) "*" if object.class.validators_on(attribute).map(&:class).include? ActiveModel::Validations::PresenceValidator end
models/user.rb
validates :email, :presence => true, :uniqueness => true, :email_format => true
validates :email, :presence => true, :uniqueness => true, :email_format => true
lib/email_format_validator.rb
class EmailFormatValidator < ActiveModel::EachValidator
def validate_each(object, attribute, value)
unless value =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
object.errors[attribute] << (options[:message] || "is not formatted properly")
end
end
end
class EmailFormatValidator < ActiveModel::EachValidator def validate_each(object, attribute, value) unless value =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i object.errors[attribute] << (options[:message] || "is not formatted properly") end end end

