New UI - Custom Submit Handler running on Cancel ?

Posted by Meryk on 30-Nov-2015 08:53

Hello,

I have a custom submitHandler I am calling on many pages (New, Edit, Quick Create). I just realized that this submitHandler function is called on Cancel too ! Which shouldn't be the case obviously ..

Here is what I am doing :

$(document).ready(function () {

var formEl = formDetails.getFormNode();

//remove existing handler...
formEl.removeAttr('onsubmit');

//add custom handler... as we want to launch ui dialog as part of handler tasks..
formEl.submit(onSubmitHandler); // onsubmitHandler defined outside

});

Is the 'Cancel' considered as a submit as well ? If yes how can we make sure the submit Handler is only called on Save and the Cancel still behaves normally ?

Thank you,

Meryem

All Replies

Posted by Mohammed Siraj on 30-Nov-2015 09:13

Right, even a cancel is actually a form submit but with particular request parameter ('act') set to identify 'Cancel' from 'Save'.

In the submit handler, you can look for the same parameter, i.e.:

var formEl = formDetails.getFormNode();

var action = formEl.find('input[name=act]').val();

if (action &&  action.toLowerCase() === 'cancel') {

//cancel

event.stopImmediatePropagation();//stop event propagation, however do not prevent form from submitting..    

}

else {

//save.. this is where your form submit handler should take over.. & do all the custom stuff

}

Posted by Meryk on 30-Nov-2015 10:22

Thank you Siraj It is working fine now :)

Cheers,

Meryem

Posted by Mohammed Siraj on 04-Dec-2015 13:28

Meryem, ideally you should not have to worry about 'Cancel' button as the Rollbase platform submit handler addresses it correctly.

Are you un-binding the default submit handler & attaching one by yourself. If so, request you to reconsider this approach. Also, for Rollbase 4.0.4 we are going to refurbish client-side API support to make it easier for end-users to configure a form submit handler &  hook into form submit event.

Posted by Meryk on 07-Dec-2015 03:14

Hi Siraj,

You are saying that if I detach the existing submit before re attaching a custom one, this should work without having to specify if the action is cancel .. ?

Even when I was doing  this :

formEl.removeAttr('onsubmit');  

formEl.submit(onSubmitHandler);

The onSubmitHandler was running on both Save and Cancel. But now with what you suggested before (checking if action === 'cancel'), the submit is only running on Save.

For the 4.0.4 change, is that mean that I need to change this code again ?

Thanks,

Meryem

Posted by Mohammed Siraj on 07-Dec-2015 03:29

Meryem, what I was suggesting was should NEVER detach any existing submit handlers. That is, avoid:

formEl.removeAttr('onsubmit');

Second, you need not change your code in 4.0.4, its just that you are duplicating 'Cancel' handling in your submit handler something which is already being addressed in the default existing submit handler.

Going forward to attach submit handlers, on the formContext object, we are providing API, i.e.:

formDetails.addSubmitHandler(onSubmitHandler);. You can leverage the same instead of :

formEl.submit(onSubmitHandler);

Once again, if you have got things working, please continue with them & you need not be concerned about any compatibility issues on upgrading to 4.0.4.

This thread is closed