You are here: Browse Railsplugins 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.
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 %>
<label for="post_user_id">Author</label>
<%= f.collection_select :user_id, @users, :id, :display_name %>
<%= f.datetime_select :published_at %>
<%= f.date_select :expires_on %>
<%= f.check_box :live %>
<%= submit_tag "Create" %>
NOTE: This description has been extracted from the Plugin README and so the formatting may need updating to make browser friendly