Browse the Ruby on Rails Community.

You are here: Browse Railsplugins Ajax Validation

Ajax Validation

This is a basic extension to provide inline AJAX powered validation of your models it is currently a very early version so there are a number of assumptions.

  • It assumes that your model/controller are singular/plural post/posts
  • It assumes you are using the form_for helper

Example

In routes.rb add the following, this extra action allows us to validate the model

map.resources :posts, :collection => [:validate]

In your controller add the following

class PostsController < ApplicationController ajax_validation_for :post ... end

Lets take a look at an example view, the new.rhtml. As you can see we are using a custom form builder ajax_validation_form_for, this will generate some html around the form elements. A number of elements will have to be defined manually however, their html should looks something like the user_id field. Note the id specified in the form_for, is referenced in the call to observe_form.

If ordered list forms aren’t your taste, it’s not to hard to override the form builder to produce something different. There is an example css file that will help as a starter.

New Post <%= error_messages_for :post %> <- ajax_validation_form_for(:post, :url => posts_url, :html => {:id => “post_form”}) do |f| -> <%= f.text_field :title %> <%= f.text_area :body %> <- end -> <%= observe_form :post_form, :url => validate_posts_path, :frequency => 2 %>

&lt;label for="post_user_id">Author&lt;/label>
    &lt;%= f.collection_select :user_id, @users, :id, :display_name %>
&lt;%= f.datetime_select :published_at %>   
&lt;%= f.date_select :expires_on %>   
&lt;%= f.check_box :live %>
&lt;%= submit_tag "Create" %>

NOTE: This description has been extracted from the Plugin README and so the formatting may need updating to make browser friendly

Users


See all 3 member details


Membership

+ Join this railsplugin

Record Maintainer

'None'