Back in Rails 2, there was a :condition option in link_to_remote which let you specify some javascript which had to return true or the remote request would not be send. However, in rails 3 it seems like this options has been removed.
What I want to do is have an ajax form, but it needs to undergo validation on the browser before it gets submitted (this is very non-standard validation). Essentially I need to be able to set some conditional javascript expression to prevent the form from being submitted if validation fails – the same as the :condition option from rails 2. Is there any way to achieve this same functionality again in rails 3?
I’m using mootools with the mootools version of rails.js. I looked through the mootools rails.js file and didn’t see anything which looked like it would let me specify some conditional javascript. Am I missing something?
*note: I just looked through the source for the default rails.js file written in prototype. In this file there are there the following lines:
var event = element.fire("ajax:before");
if (event.stopped) return false;
This looks like in the prototype version you can stop the request from being sent by listening to the “ajax:before” event and stopping the event in your listener. However, I can’t find anything like this in the mootools rails.js code, so maybe this is a bug in the mootools rails.js file?
link_to_remotewas deleted from Rails 3. It’s alink_to :remote => truenow. And if you want to do some js validation, you can use something like this:and add your validation in the application.js (in my example it’s Prototype. And it’s not a problem to do it with Mootools I guess):
update:
Mootools has this event too:
https://github.com/kevinvaldek/mootools-ujs/blob/master/Source/rails.js#L131