# Remote\_form\_for ajax validation

**URL:** <https://www.ruby-forum.com/t/remote-form-for-ajax-validation/162945>\
**Category:** Rails\
**Created:** [March 16, 2009, 2:14am UTC](https://www.ruby-forum.com/t/remote-form-for-ajax-validation/162945 "2009-03-16T02:14:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![isaac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/8e8cbc/32.png) [@isaac](https://www.ruby-forum.com/u/isaac)\
**Post date:** [March 16, 2009, 2:14am UTC](https://www.ruby-forum.com/t/remote-form-for-ajax-validation/162945/1 "2009-03-16T02:14: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| %\>  
\<%= f.error\_messages %\>

\<%= f.label :title %\>  
  
\<%= f.text\_field :title %\>

  

\<%= f.label :body %\>  
  
\<%= f.text\_area :body %\>

  

\<%= f.submit “Update” %\> ó

  
\<% end %\>

This is the controller:

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

The model:

class Product \< ActiveRecord::Base  
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:** ![isaac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/8e8cbc/32.png) [@isaac](https://www.ruby-forum.com/u/isaac)\
**Post date:** [March 25, 2009, 10:46pm UTC](https://www.ruby-forum.com/t/remote-form-for-ajax-validation/162945/2 "2009-03-25T22:46:51Z")

</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.
