How do I prevent Editor from committing changes on Ajax call during presubmit?
How do I prevent Editor from committing changes on Ajax call during presubmit?
I have read and checked the example on Editor in regards to using preSubmit
event as well as field validation. An Editor instance that I would typically create looks like this:
editor.on('presubmit', function ( event, obj, action ) {
var myEntryID = obj.data.myEntry.ID,
myStartDate = obj.data.myEntry.StartDate,
myEndDate = obj.data.myEntry.EndDate;
if (myStartDate > myEndDate) {
this.field('myEntry.StartDate').error('Start Date is to be earlier than/same as End Date');
return false;
}
However, the question that I am posting is related to an Ajax call, specifically for success
returns and how to manipulate data during the presubmit
event. The problem is that after calling the PHP script within my Ajax call, it will always return success
. And even if I can force-invoke a header('HTTP/1.0 404 Not Found')
in my PHP script upon logic conditions, I still won't be able to return false;
on error
in the Ajax section of this presubmit
event. The Editor form will still commit changes and close the form just briefly after I inserted condition checks to display error messages below some fields.
//--- Ajax call after checking initial validation in Editor on preSubmit ---//
$.ajax(
{
url: "php/myEditor_manipulateTable.php",
data: [
myEntryID: myEntryID,
myStartDate: myStartDate,
myEndDate: myEndDate
success: function(e){
if (e === "SUCCESS") {
$('#divInfo').html(e);
}
else {
editor.field('myEntry.StartDate').error('Someone has already scheduled some hours on this Start Date. Please contact the Admin!');
return false; //--- Is there a way to attach a preventDefault() here, in case return fails?
},
error: function(e){
editor.field('myEntry.StartDate').error('Someone has already scheduled some hours on this Start Date. Please contact the Admin!');
return false; //--- Is there a way to attach a preventDefault() here, in case return fails?
},
}});
I did explore using e.preventDefault
in the Ajax function, but that too didn't prevent Editor from making changes after validation on the Ajax call to the external PHP script. The reason for making this call to an external script is because I don't have the data displayed on the front-end, and the script is validating data on a different table than the one used for the front-end.
So now I would like to know if there is a way to manipulate the Ajax call in such a way that when some conditions in the external PHP script are met, I could intercept presubmit
and display the error message as well as prevent this particular Editor instance from committing changes.
This question has accepted answers - jump to:
Answers
preSubmit
is not asynchronous - you can't make an async call inside it and use the async result since the async result will return after the rest of the function has executed and returned.There are three options:
async: false
- you will get warnings in Chrome about this asasync: false
will be removed in future versions of the XHR spec.preSubmit
to handle two code paths:submit
with a flag setpreSubmit
is run again, the flag is checked (and cleared for the next time), no Ajax call is made since validation has passed.3 is probably the most complicated, but also the most complete solution.
Allan
I am trying to decipher solution 3 and am still puzzled.
When you wrote "in success", are you referring to the
success
parameter of thesubmit()
method as in the following:Or are you referring to the
success
block of the Ajax call within thefunction(dataCallback)
block?Minor update: I noticed that the flow of code is that it would execute whatever is inside the
function(dataCallback)
block except the Ajax call first, followed bypreSubmit
, then followed by the Ajax call within thefunction(dataCallback)
block. I am not sure what is the cause.Another observation: On the
submit()
code level, even if I were to call the Editor instance to display error messages when validating, usingreturn false
in any of the condition checks won't necessarily block execution. Is this expected behaviour?Neither :-). The
success
option of your$.ajax
call that you make to perform the validation.Do the Ajax validation in
preSubmit
and the flag decides the code path (i.e. has validation passed or not).Allan
This is what I have written thus far, it appears to be working on my side but I don't know if there is a cleaner way to write this (apart from the logging-to-console statements):
There is always another way to write all code - if it works and you are using the public API (which you are) then that will do nicely!
Allan
Allen,
I just don't understand what you describe as option 3 above with the flag.
Is there any way you could just show me the flow of how that would work?
Something like...
do preSubmit
validate some stuff
if flag is 1
return true
do ajax call for additional validation
on ajax success
set flag to 1
editor.submit()
return false
Thanks,
John
Hi John,
Something like:
Allan