我正在比较日期start_date和end_date的 2 个变量,并允许仅在end_date大于 start_date 的情况下提交表单,否则拒绝要提交的表单,但在运行此代码时,它会进入无限循环,如果我使用 getXMLWait() 而不是 getXML(checkDateDiff) 进行异步,则移动 API 不支持它。
此外,还有很多客户端脚本可以帮助比较日期,但移动 API 不支持这些脚本。
function onSubmit() {
var requestType = g_form.getValue('request_type');
if (requestType == 'mifi') {
console.log("calling validateTravelEndDate()");
validateTravelEndDate();
return false;
} else
return true;
}
//Helper function which calls a AJAX script include called "ClientDateTimeUtils" which gives the response in a callback where i am deciding whether to submit the form or not based on the status of days result.
function validateTravelEndDate() {
var startDate = g_form.getValue('travel_start'); //First Date/Time field
var endDate = g_form.getValue('travel_end'); //Second Date/Time field
var dttype = 'day'; //this can be day, hour, minute, second. By default it will return seconds.
console.log("startDate :" + startDate + "endDate :" + endDate);
var ajax = new GlideAjax('ClientDateTimeUtils'); // This is the script include which can be used for date validation.
ajax.addParam('sysparm_name', 'getDateTimeDiff');
ajax.addParam('sysparm_fdt', startDate);
ajax.addParam('sysparm_sdt', endDate);
ajax.addParam('sysparm_difftype', dttype);
console.log("before " + g_form.getValue('travel_end'));
ajax.getXML(checkDateDiff);
}
// callback function where deciding to go ahead or not with form submission.
function checkDateDiff(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
console.log("difference in days:" + answer);
if (answer <= 0) {
alert("Travel End date must be after Travel Start date.");
g_form.setValue('travel_end', '');
g_form.showFieldMsg('travel_end', 'Please provide a future date', 'error');
return false;
} else {
console.log("%%%%%%%%%%%%%%% Calling g_form.submit()");
g_form.submit(); // This has some issue as it’s going in the infinite loop and if we just return true/false from here as it’s asynchronous call , it’s not handled by the onSubmit function
}
}
onSubmit()
函数始终返回mifi
请求的false
。 onSubmit() 函数在返回 true
时可以执行更安全的提交。此外,g_form
函数不能在回调函数中运行,因为它是在服务器上执行的。
与其在checkDateDiff
函数的末尾有一个g_form.submit()
,不如让onSubmit()
函数返回true
。
这样的事情应该有效。我评论了我更改的每一行:
function onSubmit() {
var requestType = g_form.getValue('request_type');
if (requestType == 'mifi') {
console.log("calling validateTravelEndDate()");
// **CHANGED CODE: instead of g_form.submit(), this will return true
if(validateTravelEndDate()){
return true;
}
else{
return false;
}
} else
return true;
}
//Helper function which calls a AJAX script include called "ClientDateTimeUtils" which gives the response in a callback where i am deciding whether to submit the form or not based on the status of days result.
function validateTravelEndDate() {
var startDate = g_form.getValue('travel_start'); //First Date/Time field
var endDate = g_form.getValue('travel_end'); //Second Date/Time field
var dttype = 'day'; //this can be day, hour, minute, second. By default it will return seconds.
console.log("startDate :" + startDate + "endDate :" + endDate);
var ajax = new GlideAjax('ClientDateTimeUtils'); // This is the script include which can be used for date validation.
ajax.addParam('sysparm_name', 'getDateTimeDiff');
ajax.addParam('sysparm_fdt', startDate);
ajax.addParam('sysparm_sdt', endDate);
ajax.addParam('sysparm_difftype', dttype);
console.log("before " + g_form.getValue('travel_end'));
// **CHANGED CODE: validateTravelEndDate returns the callback value
return ajax.getXML(checkDateDiff);
}
// callback function where deciding to go ahead or not with form submission.
function checkDateDiff(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
console.log("difference in days:" + answer);
if (answer <= 0) {
alert("Travel End date must be after Travel Start date.");
g_form.setValue('travel_end', '');
g_form.showFieldMsg('travel_end', 'Please provide a future date', 'error');
return false;
}
else {
// **CHANGED CODE: checkDateDiff will return true
return true;
}
}