Client Side Validations made easy for your Rails applications!
In addition to this README please checkout the wiki Please check out the ClientSideValidations GoogleGroup
- Follow the best practices for client side validations developed by Luke Wroblewski
- Automatically extract and apply validation rules defined on the server to the client.
- In the cases where a server-side validation rule would not work on the client (i.e. conditional callbacks like :if, :unless) then do not attempt client side validations. Fall back to the server side validation.
- The client side validation error rendering should be indistinguishable from the server side validation error rendering.
- Wide browser compliancy. I've tested with IE8, seems to work OK.
- Work with any ActiveModel::Validations based model
- Validate nested fields
- Support custom validations
- Support custom FormBuilders like SimpleForm and Formtastic
- Client side validation callbacks
Include Client Side Validations in your Gemfile
gem 'client_side_validations'
Then run the install generator
rails g client_side_validations:install
This will install two files:
config/initializers/client_side_validations.rb
public/javascripts/rails.validations.js
Because the javascript file is now in the asset pipeline there is no need to rerun the generator after upgrading.
Always be sure to run
rails g client_side_validations:install
After upgrading Client Side Validations. There is a good chance that the rails.validations.js file has changed.
The javascript file is served up in the asset pipeline. Just add the
following to your app/assets/javascripts/application.js
file.
//= require rails.validations
Client Side Validations requires jQuery version >= 1.4.1
Include the rails.validations.js file in your layout
<%= javascript_include_tag 'jquery', 'rails.validations'-%>
Turn on the validations for each form_for
<%= form_for @book, :validate => true do |book| -%>
Nested fields automatically inherit the :validate value. If you want to turn it off just pass :validate => false to fields_for
<%= book.fields_for :pages, :validate => false do |page| -%>
The initializer includes a commented out ActionView::Base.field_error_proc. Uncomment this to render your error messages inline with the input fields.
I recommend you not use a solution similar to error_messages_for. Client Side Validations is never going to support rendering these type of error messages. If you want to maintain consistency between the client side rendered validation error messages and the server side rendered validation error messages please use what is in config/initializers/client_side_validations.rb
Client Side Validations supports SimpleForm:
<%= simple_form_for @book, :validate => true do |book| -%>
By default the latest version of SimpleForm will attach HTML5 Form Validators. Client Side Validations will turn off the HTML5 Form Validators if a given form is told to use Client Side Validations.
Client Side Validations supports Formtastic:
<%= semantic_form_for @book, :validate => true do |book| -%>
NOTE: Mongoid must be required before ClientSideValidations in your Gemfile.
Client Side Validations supports Mongoid >= 2.0
Anything before 2.0 won't work with Client Side Validations.
NOTE: MongoMapper must be required before ClientSideValidations in your Gemfile.
Client Side Validations supports MongoMapper >= 0.9.0
Anything before 0.9.0 won't work with Client Side Validations.
The major outstanding issue is with radio buttons. See the open ticket.
DockYard, LLC © 2012