GlideAjax and onSubmit catalog client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-25-2014 08:27 AM
Hey everyone,
Im having a difficult time with getting an onSubmit catalog client script to work properly. it is meant to validate the dates entered by the user. Im ramming my head into the wall trying to find out whats going on, so i thought that maybe someone has a reason as to why what I have wont work.
The alert on line 23 always displays true for answer and the alert on line 32 always displays null for answer.
Is there a conflict that i dont know about when it comes to onSubmit Catalog Client Scripts and Ajax calls?
function onSubmit() { //This script validates 2 things about the times entered by the user var start = g_form.getValue('start_date'); var end = g_form.getValue('end_date'); //is start before the end? var ajax = new GlideAjax('AjaxUtils'); ajax.addParam('sysparm_name','checkEndDate'); ajax.addParam('sysparm_start_date', start); ajax.addParam('sysparm_end_date', end); ajax.getXML(checkDate); //is start after now? var ajax2 = new GlideAjax('AjaxDateDiffCalc'); ajax2.addParam('sysparm_name','dateDiffNow'); ajax2.addParam('sysparm_newDate', start); ajax2.getXML(checkStart); //callback functions function checkDate(response) { var answer = response.responseXML.documentElement.getAttribute("answer"); alert ("start before end? " + answer); if (!answer) { g_form.showFieldMsg('end_date', 'The end date must occur after the start date.','error',true); return false; } } function checkStart(response) { var answer = response.responseXML.documentElement.getAttribute("answer"); alert("Start is " + answer + " seconds after now"); if (answer <= 0) { g_form.showFieldMsg('start_date', 'The start date must not be set to a time prior to now.','error',true); return false; } } }
the previously existing Script includes that i was attempting to use are as follows:
AjaxDateDiffCalc: var AjaxDateDiffCalc = Class.create(); AjaxDateDiffCalc.prototype = Object.extendsObject(AbstractAjaxProcessor, { dateDiffNow: function() { var diff = gs.dateDiff(gs.nowDateTime(), this.getParameter('sysparm_newDate'), true); return diff; },
and
checkEndDate : function(startDate, endDate) { if (typeof(startDate) == 'undefined') { startDate = this.getParameter('sysparm_start_date'); } if (typeof(endDate) == 'undefined') { endDate = this.getParameter('sysparm_end_date'); } return(gs.dateDiff(startDate, endDate, true) >= 0); },
This is from the class 'AjaxUtils'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-25-2014 04:03 PM
try: getXMLWait(checkDate);
I've tried doing Ajax calls in onSubmits in the past without much luck.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-14-2014 10:47 AM
I'm having a very similar issue. I have an onSubmit client script where I'm trying to validate a number of date fields. If I use the normal getXMLwait() the Client Script runs perfectly, but any time I try to add a callback it throws an error. Spent most of the day working on this yesterday without much luck so I've taken to the Community for some advice.
Looks like I'm not the only one running across this though, anyone have any advice?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-14-2014 12:11 PM
Hi,
As said before, onSubmit does not care about callbacks. If you really needs something to be computed on the server and verified before submitting, you should be better off by validating by onChange and hidden form variables. The onSubmit would then only need to work with the hidden variables.
In this example, you might want to add a hidden variable for storing the last validated date values and then onSubmit compare if the last validated value is the same as the user entered or something like that.
Kind regards,
Dan Berglin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-14-2014 12:56 PM
OK, thanks for the insight. I think I'm just going to reevaluate and think how I can get the same result another way. Thanks!