# RJS and Form Validation (Best Practice)

**URL:** <https://www.ruby-forum.com/t/rjs-and-form-validation-best-practice/55777>\
**Category:** Rails\
**Created:** [March 23, 2006, 7:37pm UTC](https://www.ruby-forum.com/t/rjs-and-form-validation-best-practice/55777 "2006-03-23T19:37:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dylan\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/34f0e0/32.png) [@Dylan\_S](https://www.ruby-forum.com/u/Dylan_S)\
**Post date:** [March 23, 2006, 7:37pm UTC](https://www.ruby-forum.com/t/rjs-and-form-validation-best-practice/55777/1 "2006-03-23T19:37:48Z")

</div>

Hello All !

I’ve been using RJS extensively, but haven’t done any form validation  
with  
it until now.

Should I be manually passing the RJS template some models error messages  
from the controller… or  
… should I be using error\_messages\_for in order to do the display ?

# I’ve implemented the first approach, but it seems like quite a hack. Any insight would be greatly appreciated ! Thanks !

Dylan

---

<div class="post-metadata">

**Author:** ![Dylan\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/34f0e0/32.png) [@Dylan\_S](https://www.ruby-forum.com/u/Dylan_S)\
**Post date:** [March 23, 2006, 9:53pm UTC](https://www.ruby-forum.com/t/rjs-and-form-validation-best-practice/55777/2 "2006-03-23T21:53:26Z")

</div>

I do it the same as with non-ajax forms, just throw your \<%=  
error\_messages\_for :model %\> at the top of a form partial  
If there’s an error creating, or updating the model, just  
page.replace\_html’id\_of\_el\_surrounding\_form\_partial’, :partial =\>  
‘form’  
That way you still get highlighting for the failed fields, and it makes  
it  
easy to provide a fallback if javascript is disabled.

joshua

---

<div class="post-metadata">

**Author:** ![Dylan\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/34f0e0/32.png) [@Dylan\_S](https://www.ruby-forum.com/u/Dylan_S)\
**Post date:** [March 24, 2006, 7:09pm UTC](https://www.ruby-forum.com/t/rjs-and-form-validation-best-practice/55777/3 "2006-03-24T19:09:57Z")

</div>

Thanks Joshua, this works perfectly.  
In my case, the :partial is actually a component… and… haven’t  
checked  
if RJS supports :component… hmmm… I’m lazy 🙂

---

<div class="post-metadata">

**Author:** ![Dylan\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/34f0e0/32.png) [@Dylan\_S](https://www.ruby-forum.com/u/Dylan_S)\
**Post date:** [March 24, 2006, 8:23pm UTC](https://www.ruby-forum.com/t/rjs-and-form-validation-best-practice/55777/4 "2006-03-24T20:23:29Z")

</div>

I have a feeling RJS won’t support components, and even if it did I  
would  
avoid using them.  
Check out  
[http://railsexpress.de/blog/articles/2005/11/18/components-may-not-be-evil-but-they-sure-can-be-slow](http://railsexpress.de/blog/articles/2005/11/18/components-may-not-be-evil-but-they-sure-can-be-slow)  
You want those ajax calls to be snappy!

joshua

---

<div class="post-metadata">

**Author:** ![Dylan\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/34f0e0/32.png) [@Dylan\_S](https://www.ruby-forum.com/u/Dylan_S)\
**Post date:** [May 23, 2006, 10:05pm UTC](https://www.ruby-forum.com/t/rjs-and-form-validation-best-practice/55777/5 "2006-05-23T22:05:23Z")

</div>

Hi Dylan,

I added a

&nbsp;
 just  
ahead of my form\_remote\_to tag helper.

Then in the action method of the form (create.rjs in my case:

if @client.errors.length \> 0  
page.replace\_html ‘flash\_box’, error\_messages\_for(:client)  
page.show ‘flash\_box’  
else  
page.hide ‘flash\_box’  
end

I got the hint from liquid’s comment to  
[http://rails.techno-weenie.net/question/2006/2/23/ajax\_validation\_flash\_messages\_redirects](http://rails.techno-weenie.net/question/2006/2/23/ajax_validation_flash_messages_redirects)

Ed
