Last Updated: February 25, 2016
· yottanami

Ruby on Rails client side validation

For client side form validation in ROR you can use

First add this lines in your GemFIle :

gem 'jquery-rails'  
gem 'client_side_validations' 

And then

bundle install

Now use the generator

rails g client_side_validations:install

You should include rails.validations JavaScript files.

<%= javascript_include_tag :defaults, "rails.validations" %>  

uncomment these lines of /config/initializers/client_side_validations.rb

# ClientSideValidations Initializer  

require 'client_side_validations/simple_form' if defined?(::SimpleForm)  
require 'client_side_validations/formtastic' if defined?(::Formtastic)  

# Uncomment the following block if you want each input field to have the validation messages attached.  
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|  
  unless html_tag =~ /^<label/  
     %{<div class="field_with_errors">#{html_tag}<label for="#{instance.send(:tag_id)}"  
  class="message">#{instance.error_message.first}</label> </div>}.html_safe  
    %{<div class="field_with_errors">#{html_tag}</div>}.html_safe  

Now all we need is adding :validate => true in form tags

2 Responses
Add your response


client-side-validations gem is no longer maintained and in Rails 4.1 it gave me quite extraordinary errors.

over 1 year ago ·

Yes as Gihub page said : "This library is no longer being maintained. Use at your own risk"

You can use rails4clientsidevalidations or https://github.com/amatsuda/html5validators

over 1 year ago ·