Last week I found a little caveat when using the client_side_validations gem with ajax-loaded forms. It didn’t work out of the box.
Digging through the sourcecode I found this comment
1 2 3 4 | |
Ok, so I called $("#my-form").validate() on ajax:success and everything was fine! Except it wasn’t =/
The behavior I understood, after banging my head against the wall several times was: if the form was entirely rendered in my ajax call it would validate ok, but if I added some fields inside it dynamically then I’d start getting those nasty validators is not defined errors and everything would go boom!
So how do I add some fields to the form and keep them “validatable”?
this issue gave me some insight. And after rummaging the code a little more I found that this gem registers the validators in the window object. This process is done inside the form renderer, and that’s why the full rendered forms worked and adding just some of the fields did not. To accomplish that I created this helper
1 2 3 4 5 6 7 8 9 | |
It recieves an array of form builders and inspect it for the validators added by the gem. Then it prints the resulting instruction within the form partial, inside a function, to which I pass the id of the form that I want to validate.
1 2 3 4 | |
Then after the fields (with the script block) were added the to form you can call
1 2 | |
I know this is hacky as hell, but with this I was able to keep using this gem even inside pages with lots of moving parts.