Now you can easily drop in client side validations in any Rails app. It will use validations defined in a given ActiveRecord (or ActiveModel) class for use with jquery-validation
For Rails 2 and Rails 3 example apps please see client_side_validations_example
The concept is simple:
- Include the middleware
- Define validations in the model as you normally would
- The validations are sent to the client in JSON
- client_side_validations.js converts the JSON for a given validation plugin and binds the validator to the form
Currently the following validations are supported:
- validates_presence_of
- validates_format_of
- validates_numericality_of
- validates_length_of
- validates_size_of
- validates_uniqueness_of
- validates_confirmation_of
- validates_acceptance_of
- validates_inclusion_of
- validates_exclusion_of
The uniqueness validation works for both ActiveRecord and Mongoid.
gem install client_side_validations
Add "config.gem :client_side_validations" to the "config/environment.rb" file
Then run the generator:
script/generate client_side_validations
This will copy client_side_validations.js to "public/javascripts"
This version of ClientSideValidations will also copy a patched version of jquery-validation.js to "public/javascript"
Add "gem 'client_side_validations'" to the Gemfile
Then run the generator:
rails g client_side_validations
This will copy client_side_validations.js to "public/javascripts"
This version of ClientSideValidations will also copy a patched version of jquery-validation.js to "public/javascript"
NOTE This version of ClientSideValidations has a patched version of jquery-validation that will install automatically with the generator. Do not download the version listed below.
Download jQuery and jQuery Validation plugin to "public/javascripts"
As of version 2.9.0 the ClientSideValidations::Uniqueness middleware is automatically included as a Rails Engine. (both Rails 2 and Rails 3)
Validate your models as you normally would
class Book < ActiveRecord::Base
validates_presence_of :author
end
You currently need both jQuery and the jQuery Validate plugin loaded before you load Client Side Validations
...
<%= javascript_include_tag 'jquery', 'jquery-validation', 'client_side_validations' %>
...
Have a form ask for client side validations by passing :validate => true
...
<% form_for @book, :validations => true do |b| %>
<%= b.label :author %></br>
<%= b.text_field :author %></br>
<%= submit_tag 'Create' %>
<% end %>
...
That should be it!
jquery-validation can be customized by setting various options
Create config/initializers/client_side_validations.rb
An example set of default options can look like:
ClientSideValidations.default_options = {
:onkeyup => false,
:errorClass => "validation_errors"
}
If you want to define only specific fields for client side validations just override the validation_fields method on each model
class Book < ActiveRecord::Base
validatese_presence_of :author
validates_presence_of :body
private
def validation_fields
[:author]
end
end
You can override the default options set in the initializer for each form:
<% form_for @book, :validations => { :options => { :errorClass => "bad-field" } } do |b| %>
...
If you are not using an instance variable for form_for or for some reason want to use the validations from another class that can be done in two ways:
<% form_for :book, :validations => Book %>
...
<% form_for :book, :validations => { :class => Book } %>
...
Written by Brian Cardarella
Copyright (c) 2010 Democratic National Committee. See LICENSE for details.