Easy Client-Side Form Validation for Rails 3.1

Easy client-side form validation in rails 3.1

Form validation can be annoying, with all the error message styling and all. Heres an easy solution I just stumbled across. Note that this works best for when starting new apps, but should still plugin quite nicely into old ones.

Setup:
I will be using this javascript library for validation. I use the styling from Bootstrap to make it look extra pro.

Get the Javascript file

Here is a slightly nerdy terminal way of getting it. I like it because when you use it again in future you just can go “git pull” and get the latest version.

# Terminal

$ cd into your favourite place to keep jquery plugins
$ git clone https://github.com/victorjonsson/jQuery-Form-Validator.git
$ cd jQuery-Form-Validator/
$ cp jquery.formvalidator.min.js path-to-rails-app/vendor/assets/javascripts/

# application.js
...
#= require jquery.formvalidator.min.js
...

Styling

/* Bootstrap styling is optional. If you're starting from scratch (no previous form styling) then I'd use it. */

/* application.css */
...
*= require bootstrap
...

/* Here is some extra styling for the plugin. Basically it puts the error message just below the text field. */

/* styles.css.scss */
.jquery_form_error_message{
  display: block;
  margin-top: 3px;
}

The form markup

I’d only look at this if you are using Bootstrap, as this is based on their CSS.

Notice the data-validation attribute in the input element? This is where you specify cool validation stuff – check out the github page for a list of options.

<form action="/users" id="signup_form">
      <fieldset>
        <legend>Sign up</legend>
        <div class="clearfix">
          <label for="email_field">Email</label>
          <div class="input">
            <input data-validation="validate_email" id="email_field" type="text" name="user[email]" value="" placeholder="Email">
          </div>
        </div>

        <div class="clearfix">
          <label for="first_name_field">First Name</label>
          <div class="input">
            <input data-validation="required validate_min_length length4" id="first_name_field" type="text" name="user[first_name]" value="" placeholder="First name">
          </div>
        </div>

etc...

The Javascript

$('#my_form').validateOnBlur()

About the Author

Plattsi | Other Articles

A twenty something web developer and entrepreneur from Sydney, Australia. Loves building web applications that are both easy and fun to use (and don't require manuals).

  • http://twitter.com/jwo Jesse Wolgamott

    Quick note — in your terminal syntax you clone “git clone https://github.com/codebrew/backbone-rails.git” but I think you want this “git clone git://github.com/victorjonsson/jQuery-Form-Validator.git”

  • http://webtempest.com Web Tempest

    Thanks Jesse 

  • erwin

    That’s great … at least one simple and clear plugin … for Rails 3.1 lovers
    however, some funny developers also write code for non-english people, and then need to have localized messages…  is there any way to relate messages to the Rails I18n.locale ?

    additional question :  in a current Rails app under development, I display all error messages close to the label of the field  using a span :
     #{instance.error_message.join(‘,’)})
    is there any way to display client js errors the same way, so the user will see a unified error message behavior ?

    erwin

  • http://webtempest.com Web Tempest

    The messages are generated by the jQuery plugin, and have nothing to do with Rails. The title of this post is rather misleading actually – as it could be used in any framework. So to get the Rails l18n stuff you would be better off ignoring any javascripty client side validation such as this, and do it all through Rails. 

    For your second question, to achieve that it would probably be faster to create write your own custom javascript … i.e perhaps do an ajax call on submit which returns an array of errors that you can show as error messages where ever you want.

  • erwin

    Thanks a lot for your reply

    regarding Rails I18n => javascript I18n , I found an interesting gem , 
    https://github.com/fnando/i18n-js as quoted per his creator :
    “It’s a small library to provide the Rails I18n translations on the Javascript.”

  • http://webtempest.com Web Tempest

    Ah yep nice find. 

  • Err

    dumb question, im using boostrap with html only no rails, so what do i need to validate the form? Where do i add   $(‘#my_form’).validateOnBlur() ?

  • http://webtempest.com Web Tempest

    Just before put this:

    $(‘#my_form’).validateOnBlur();

  • zhanhua

    Hello, could you send me whole source of this example to me?
    I can not let it run under Rails 3.2

  • http://webtempest.com Web Tempest

    This wasn’t tested using rails 3.2…what error are you getting?

  • Chetan Kalore

    Hi, From this statement return $(this).validate()); I am always getting [Object, Object] instead of true or false. Can you please help me why this is happening. It’s urgent for me.

    I have followed all the steps, only the problem is in case of errors still form is getting submitted.

  • http://webtempest.com Web Tempest

    I would need more information to help with debugging sorry. Guessing you are doing something like ? If so then you may need to raise an issue in Github as that is the correct way of doing it.