views:

70

answers:

2

I have a situation where I have a form with multiple submit buttons and I want to update a remote frame. I've tried using a g:formremote with 2 g:actionsubmit buttons (which support javascript) but the multiple submit buttons have a glitch (described here: http://www.grails.org/Ajax under "Multiple buttons with formRemote").

I took the workaround, using 2 g:submittoremote buttons, which work the way I expect, but doesn't accept the javascript parameters like onClick (the buttons in question are accept/reject and I want to put an AYS on the reject so it isn't used accidentally).

Is there a way for javascript and multiple submit buttons in a remote form to exist peacefully?

Thanks in advance for your help...

+1  A: 

Okay, I'm not saying this is beautiful, but I just messed around with this for a few minutes and have something that might work for you. Like I said... not the prettiest solution, but workarounds rarely are...

 <html>
<head>
<g:javascript library="prototype" />
</head>
<body>
<script type="text/JavaScript">
function setReject()
{
  document.getElementById('reject').value='true'
}
</script>

<g:formRemote name="test" update="updater" url="[ controller: 'date', action: 'test']" >
    <g:hiddenField name="reject" value="false" />
    <g:submitButton name="submit" value="something" onclick="setReject();return confirm('Are you sure???')" />
    <g:submitToRemote update="updater" action="otherTest" controller="date" value="Go Ahead"/>
</g:formRemote>


<div id="updater">

</div>
</body>
</html>
proflux
+3  A: 

Did you try the before parameter? It takes a javascript function, which will be executed before the remote funtion call. Just use it like this:

<g:submitToRemote value="Reject" update="feedback" 
                  controller="test" action="reject"
                  before="if (!confirm('sure?')) {return false;}" />

Whatever javascript you put in the before parameter will be inserted into the onclick attribute rigth before your Ajax updater call. This way you can easily do validation, get confirmations etc. and even break from onclick handling before submitting the Ajax call. There is a similiar after parameter.

hlg
I was messing with a bunch of permutations of the before attribute but didn't try this one. This should do exactly what the OP wants. Good call!
proflux
Simple, clean, and works like a charm! Thank you SO much!
pennstatephil
ETA: On further inspection, this still sends the form regardless of the user's choice. I found a solution on a programmer's blog (http://fbflex.wordpress.com/2010/05/09/adding-javascript-validation-to-formremote-ajax-forms-in-grails/); the gist of it is to close the "if" call in an `after`, so it'd be:<g:submitToRemote value="Reject" update="feedback" controller="test" action="reject" before="if (!confirm('sure?')) {return false;" after="}" />This worked for me!
pennstatephil
If the confirmation box is canceled, the form is not send and the action doesn't get executed, however if it is confirmed with OK, the form is sent to the reject action. Tested and doublechecked with current firefox and IE.You could rewrite your latter solution to before="if(confirm('sure?')){" after="}" but it's odd to split the statement across parameters, especially as the submitToRemote tag inserts semicolons at the end of before and after. You can see that in the source of the rendered page.
hlg