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 McGradyAdditional comments powered by BackType
Follow my RSS Feed
10 Comments
Simple live validations in rails http://bit.ly/mew7l #rails
This comment was originally posted on Twitter
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.
Simple Live Validations in Rails | dMix | Dan McGrady http://bit.ly/mew7l
This comment was originally posted on Twitter
live validations with ruby on rails http://bit.ly/b7xE4
#rails #ror
This comment was originally posted on Twitter
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.
Doh, the pastie didn’t get included, just follow this link http://pastie.org/542319
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.
@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.
@Dan: Oh yeah, you’re right – I mixed that up with periodically_call_remote!
Simple Live Validations in Rails http://bit.ly/b7xE4
This comment was originally posted on Twitter