onsubmit
My code always return true,
fd.onsubmit(function () {
var checkDuplicate = itemIsDuplicate();
checkDuplicate
.done(function(isDuplicate) {
if(isDuplicate) {
return false;
}
});
return true;
});
function itemIsDuplicate() {
var deferred = $.Deferred();
var isDuplicate = false;
var ctx = SP.ClientContext.get_current(),
targetList = ctx.get_web().get_lists().getByTitle("BookingTest"),
frDate = fd.field('EventDate').control().value()[0].toString().substring(6,10)+"-"+fd.field('EventDate').control().value()[0].toString().substring(3,5)+"-"+fd.field('EventDate').control().value()[0].toString().substring(0,2)+"T"+fd.field('EventDate').control().value()[1].toString().substring(0,2)+":"+fd.field('EventDate').control().value()[2].toString()+":00Z",
tDate = fd.field('EndDate').control().value()[0].toString().substring(6,10)+"-"+fd.field('EndDate').control().value()[0].toString().substring(3,5)+"-"+fd.field('EndDate').control().value()[0].toString().substring(0,2)+"T"+fd.field('EndDate').control().value()[1].toString().substring(0,2)+":"+fd.field('EndDate').control().value()[2].toString()+":00Z",
camlQuery = new SP.CamlQuery(),
collListItem, strViewXml;
strViewXml = "<View><Query><Where><And><Lt><FieldRef Name='EventDate' />";
strViewXml += "<Value IncludeTimeValue='TRUE' Type='DateTime'>" + tDate + "</Value></Lt><Gt><FieldRef Name='EndDate' />";
strViewXml += "<Value IncludeTimeValue='TRUE' Type='DateTime'>" + frDate + "</Value></Gt></And></Where></Query></View>";
camlQuery.set_viewXml(strViewXml);
collListItem = targetList.getItems(camlQuery);
ctx.load(collListItem);
ctx.executeQueryAsync(
function(){deferred.resolve(collListItem.get_count() > 0); },
function(sender, args) {deferred.reject(sender, args.get_message()); }
);
return deferred.promise();
}
fd.onsubmit(function () {
var checkDuplicate = itemIsDuplicate();
checkDuplicate
.done(function(isDuplicate) {
if(isDuplicate) {
return false;
}
});
return true;
});
function itemIsDuplicate() {
var deferred = $.Deferred();
var isDuplicate = false;
var ctx = SP.ClientContext.get_current(),
targetList = ctx.get_web().get_lists().getByTitle("BookingTest"),
frDate = fd.field('EventDate').control().value()[0].toString().substring(6,10)+"-"+fd.field('EventDate').control().value()[0].toString().substring(3,5)+"-"+fd.field('EventDate').control().value()[0].toString().substring(0,2)+"T"+fd.field('EventDate').control().value()[1].toString().substring(0,2)+":"+fd.field('EventDate').control().value()[2].toString()+":00Z",
tDate = fd.field('EndDate').control().value()[0].toString().substring(6,10)+"-"+fd.field('EndDate').control().value()[0].toString().substring(3,5)+"-"+fd.field('EndDate').control().value()[0].toString().substring(0,2)+"T"+fd.field('EndDate').control().value()[1].toString().substring(0,2)+":"+fd.field('EndDate').control().value()[2].toString()+":00Z",
camlQuery = new SP.CamlQuery(),
collListItem, strViewXml;
strViewXml = "<View><Query><Where><And><Lt><FieldRef Name='EventDate' />";
strViewXml += "<Value IncludeTimeValue='TRUE' Type='DateTime'>" + tDate + "</Value></Lt><Gt><FieldRef Name='EndDate' />";
strViewXml += "<Value IncludeTimeValue='TRUE' Type='DateTime'>" + frDate + "</Value></Gt></And></Where></Query></View>";
camlQuery.set_viewXml(strViewXml);
collListItem = targetList.getItems(camlQuery);
ctx.load(collListItem);
ctx.executeQueryAsync(
function(){deferred.resolve(collListItem.get_count() > 0); },
function(sender, args) {deferred.reject(sender, args.get_message()); }
);
return deferred.promise();
}
What you are doing is using the jQuery deferred object. From https://api.jquery.com/category/deferred-object/: "It can register multiple callbacks into callback queues, invoke callback queues, and relay the success or failure state of any synchronous or asynchronous function.". Basically, it is something that allows you to execute stuff asynchronously via callbacks.
What you do with
Is attach a callback function that will be executed when the deferred object is resolved. After you attach that callback function you return true and exit from the onsubmit function. When deferred is resolved, the .done callback is executed returning true -- but it's not returning true to the onsubmit function, as the onsubmit function is not the calling function and it has already finished executing anyway.
Now, what you can do is this:
1. Add the save button to Forms Designer in design mode (click the "Buttons" button at the top of FD)
2. Place it somewhere and hide it (with CSS).
3. Add another (regular, not "save") button onto the form, call it "Submit"
4. Add code to it that will do your stuff, i.e.
My advice for future would be to use debugger when writing code. Simply write "debugger;" in code where you want to start debugging and open console in your browser - you will see exactly in what order you code is being executed.
What you do with
Code: Select all
checkDuplicate.done(function(isDuplicate) {
if(isDuplicate) {
return false;
}
});
Now, what you can do is this:
1. Add the save button to Forms Designer in design mode (click the "Buttons" button at the top of FD)
2. Place it somewhere and hide it (with CSS).
3. Add another (regular, not "save") button onto the form, call it "Submit"
4. Add code to it that will do your stuff, i.e.
Code: Select all
itemIsDuplicate();
function itemIsDuplicate() {
.........
ctx.executeQueryAsync(
function(){
fd.save().click();
},
function(sender, args) {
alert("something's incorrect!")
}
);
}
My advice for future would be to use debugger when writing code. Simply write "debugger;" in code where you want to start debugging and open console in your browser - you will see exactly in what order you code is being executed.
-
- Information
-
Who is online
Users browsing this forum: No registered users and 13 guests