Search |
||
Autovalidation with RailsPosted by bleonard on April 16, 2008 at 12:31 PM PDT
Actually, I don't know what this feature is called, but I love it when a web application gives me instant feedback on the validity of my entry. For example:
![]() This is especially true when I'm registering a new user account - submitting the page over and over again because the username I've selected is already taken or the password doesn't meet the requirements is always a drag. So I started looked for examples on how to do this in Rails and couldn't really find anything (possibly because I don't actually know what to call it) - I guess it's most similar to autocompletion, so I'm going with autovalidation. And here are the steps I've put together to get it to work... Setting Things UpCreating the Rails Project
Traditional Server Side Validation
Lets AutovalidateIn addition to the server side validation that's being performed, we're going to add some client side validation using Ajax. We'll do this by displaying a message to the right of the field on the validity of what's been entered. Since we're validating against the database, we will need to make an XMLHTTPRequest back to the server. We can use Rails' observe_field helper method for this task.
Run with Autovalidate
TroubleshootingFirebug is your friend for troubleshooting Rails applications. With Firebug enabled, you can see the requests as you type:
A couple of things that tripped me up
The Completed Application»
Related Topics >>
Netbeans Comments
Comments are listed in date ascending order (oldest first)
Submitted by bleonard on Fri, 2008-10-03 08:57.
Sure, send me an updated zip and I'll post it to the site. /Brian
Submitted by neilcauldwell on Fri, 2008-10-03 10:07.
Brian, I couldn't work out how to contact you on email so, I've uploaded it somewhere;
http://www.4shared.com/file/65533323/690c6db9/autovalidation.html
I had to quickly go over it again because I had it set up for sqlite (really easy db setup with Rails, now the default for the framework) but I'm confident it's good to go.
Submitted by bleonard on Fri, 2008-10-03 11:31.
Usually @java.net will work. No worries, I've just uploaded your version of the project. Thanks!
Submitted by neilcauldwell on Fri, 2008-10-03 01:11.
Thanks Brian. I downloaded the project - there were a couple of differences between the code in the zip and the tutorial (the span id="message" had a couple of characters inside, and the :validate action had a 'puts #{user.username}" which wasn't in the tutorial).
I realised that my user controller was running a before filter (a require login) which obviously prevented the ajax request from working (doh). I didn't check to see if it was this specifically (I didn't make a comparison between the code in the tutorial and the download, but I'm 99% sure it was my before filter).
Would you like me to send you a zip with the exact code from the tutorial?
Submitted by neilcauldwell on Sat, 2008-09-27 14:11.
Brian, this looks great, I'm just struggling with a weird result in which the update seems to be rendering an entirely new body tag within the span. I can see the validate requests working, it just seems to being putting a whole new request inside the span...any ideas on how to fix this?
|
||
|
|