2009
07
July

Simple Live Validations in Rails

The first day of working with NuLayer, I had the task of adding live validations to a signup form, something very lightweight and clean. Although there are a few plugins (see below), hand coding this into a rails app is really simple.

The Scenario: Validate the username is unique as it’s typed into the forms text field

1) Live_validations controller and validation method

Create a controller to store your validation methods and add the appropriate routes.

2) Message partial

Create a partial that will handle the HTML that is returned for the ajax call.

In your form add an observe_field helper (JRails or Prototype required). This helper will observe the field every 0.5 seconds and pass the value of the text_field via params[:username] to the live_validation controller method.


I used accept.png and cross.png icons from Fam Fam.

Done.

JQuery + Rails Plugin Options

For straight javascript there are a few options, such as jQuery Validation and LiveValidations.com; these are great for basic validations (ex. email has a proper format) but they don’t have access to ActiveRecord.

There is also a rails plugin LiveValidations that hooks up to your existing validations and comes with a form builder for the AJAX, but after testing it: it doesn’t work with nested forms and with hundreds of lines of code it seemed like overkill.

- By Dan McGrady

4 Tweets 6 Other Comments

10 Comments

  1. BrianTheCoder
    Posted July 10, 2009 at 3:06 pm | Permalink

    Why not just return true or false and have javascript handle the rest? For ajax calls its usually best to have rails do as little as possible to make it fast.

  2. Posted July 11, 2009 at 8:40 am | Permalink

    I too wanted to have some live validation in my rails app, I didn’t want to duplicate the validation definitions on the client side, instead I wrote a controller method that would validate a form, and respond with either a success or failure, as well as all validation error messages.

    This can then get called as frequently as you like when the user types etc. I’m sure it could probably be made quicker with metal, and using some magic be able to handle any models, but for now it works.

  3. Posted July 11, 2009 at 8:41 am | Permalink

    Doh, the pastie didn’t get included, just follow this link http://pastie.org/542319

  4. Posted July 11, 2009 at 8:52 am | Permalink

    So this will flood your server with 2 ajax requests per form field per user – say you’ve got 50 users on the signup form you’ll have 300 ajax calls per second, even when the users just have the page open and aren’t doing anything. You really should switch that to some on-change-only JS behaviour. I’d use onblur here, which will trigger when the form field is left. Also, I don’t see the point in using a partial for rendering plain text here. I’d switch that to render :text => @message. I’d also make use of :status => 2XX and 4XX and let the JS handle the icon and text color stuff.

  5. Posted July 11, 2009 at 9:20 am | Permalink

    @BrianTheCoder: I definitely plan on refactoring it to minimize the amount of data that rails is returning and let Javascript handle the rest.

    @Oliver: thanks, we actually used something similar in another method (User.new = valid?), for demonstration I chose the simplest one. The JSON response looks clean.

    @Christoph Olszowka: keep in mind it only sends requests if something is typed. If 50 users are on the page, only a fraction of the time is spent typing the username. Although 500 users is another story and would definitely benefit from the refactoring mentioned above.

  6. Posted July 12, 2009 at 6:24 am | Permalink

    @Dan: Oh yeah, you’re right – I mixed that up with periodically_call_remote!

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

Additional comments powered by BackType