# remote\_form\_for ajax validation

**URL:** <https://discuss.rubyonrails.org/t/remote-form-for-ajax-validation/40466>\
**Category:** rubyonrails-talk\
**Created:** [March 15, 2009, 10:59pm UTC](https://discuss.rubyonrails.org/t/remote-form-for-ajax-validation/40466 "2009-03-15T22:59:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Isaac1](https://avatars.discourse-cdn.com/v4/letter/i/e79b87/32.png) [@Isaac1](https://discuss.rubyonrails.org/u/Isaac1)\
**Post date:** [March 15, 2009, 10:59pm UTC](https://discuss.rubyonrails.org/t/remote-form-for-ajax-validation/40466/1 "2009-03-15T22:59:40Z")

</div>

Hi,

I have been reading a lot about this, but I do not manage to do a nice validation. I already managed to save data to the database, to validate a field, to display a "Everything went well" message. I can also display an error message, but the view does not end up properly.

This is the form:

\<% remote\_form\_for(product) do |f| %\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.error\_messages %\> &nbsp;&nbsp;&nbsp;&nbsp;\<p\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= f.label :title %\>\<br /\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= f.text\_field :title %\> &nbsp;&nbsp;&nbsp;&nbsp;\</p\> &nbsp;&nbsp;&nbsp;&nbsp;\<p\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= f.label :body %\>\<br /\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= f.text\_area :body %\> &nbsp;&nbsp;&nbsp;&nbsp;\</p\> &nbsp;&nbsp;&nbsp;&nbsp;\<p\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= f.submit "Update" %\> ó &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</p\> &nbsp;&nbsp;\<% end %\>

This is the controller:

def update &nbsp;&nbsp;&nbsp;&nbsp;@product = Product.find(params[:id]) &nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if @product.update\_attributes(params[:product]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = 'OK!' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html { redirect\_to(@product) } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.js &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash.now[:notice] = 'Error!!' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html { render :action =\> "show" } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { render :xml =\> @product.errors, :status =\> :unprocessable\_entity } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.js &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

The model:

class Product \< ActiveRecord::Base &nbsp;&nbsp;validates\_presence\_of :title end

And the RJS template:

page.replace\_html :description, :partial =\> "product", :object =\> @product page.replace\_html :notice, flash[:notice] page.visual\_effect (:fade, "notice", :duration =\> 2) flash.discard

But I thing there is a better way to validate forms.

Does any one have an example about validating, and displaying customized messages.

Br,

Isaac

---

<div class="post-metadata">

**Author:** ![eric2](https://avatars.discourse-cdn.com/v4/letter/e/8c91f0/32.png) [@eric2](https://discuss.rubyonrails.org/u/eric2)\
**Post date:** [March 25, 2009, 8:22pm UTC](https://discuss.rubyonrails.org/t/remote-form-for-ajax-validation/40466/2 "2009-03-25T20:22:33Z")

</div>

Like commonly with regular forms, I prefer javascript validation before the form submits. I happened across what I think is a valid method.

\<% remote\_form\_for(product, :before =\> "if(!validateForm(this)) { return false; }") %\>

Where validateForm is a javascript function that does the form validation. Note that using this method, the javascript function MUST return a boolean, either true for valid data, or false if not. If it's not valid then the form won't submit. Within the javascript you can put alerts or change the html with error messages so that the user can correct their input data.
