How to restrict a form to be submitted with the client script along with GlideAjax?

Moon1
Giga Contributor

I found few other thread about similar issue and answered, but not exactly what I am looking at.

I want to know how to prevent to submit/to order a form (catalog item) using the Catalog Client Script.

I tested 'return false;' on onSubmit event on the varaible set.

My issue is to link that with the GlideAjax function call and on the handler of the Ajax call through getXML (Asyn handler).

I can't make it work using 'return false' inside on the handler of the response.

Thank you for helping on this. I may make it work with Business Rule, but I want to run about the client side to handle GlideAjax handler more..

--- example   code snippet--

function onSubmit() {

    // Execute my Ajax

    var gajax = new GlideAjax('myScriptIncludeClass');

//... send parameter here.

    gajax.getXML(responseHandler);

}

// below is the asyn call handler

function responseHandler(serverResponse) {

     var result = serverResponse.responseXML.getElementsByTagName("result");

 

     if (result[0].getAttribute('valid') == 'false') {

        // TODO... display warn msg and prevent to submit

        alert('Invalid Form value. please fix before order the request');

       //BElow is NOT working... HELP !!!

         return false;

     }

}

4 REPLIES 4

asifnoor
Kilo Patron

Hi

GlideAJax works asynchronously hence the form will not wait till you get the response from GlideAjax.

So you need to call GlideAjax in the onchange event, instead of onSubmit and get the value and store in some form field which is hidden.

In onsubmit, check the value of that hidden form field and return true or false based on that.

Mark the comment as a correct answer and also helpful if this answers your question.

Moon1
Giga Contributor

thank you for a quick response and your suggestion(Idea).

To make it more clear, I have four variables on the variableSet which uses on the catalog item.

The validation needs to be completed against those four variable values against one table which contains the record to check against the four variable values.

 

Having said and with your idea using onChange() of all four variables, and update hidden variable which contains those four variable values combination is valid or not on onSubmit() - if (g_form.getValue('hidden_validation') == 'false') { //Do something I want to};?

Is it what you think about?

Munhwan 

 

Yes correct.

however, you don't need to write Onchange on all 4 variables. Assuming that you plan to write this in onsubmit, which means the validation of all these 4 values to be done at once.

So in the order of display of these 4 fields, write on change on the last field and in that check if the first 3 fields are filled or not. if not filled raise client error and return. if all filled, then you can call glide ajax and based on the output, you store the value in the hidden field.

And then in onsubmit, check that hidden value.

NOTE: The field that you plan to hide should be added to the form and onload, make the display false using g_form.setDisplay('field_name',false) to hide. Only then you can read the value of the form field in onsubmit using g_form.getValue or you can set using g_form.setValue.

Mark the comment as a correct answer and also helpful if it helps.

Harish Murikina
Tera Guru

Hi,

 

GetXML is asynchronous so it wont work in onsubmit, the reason behind it , when you click submit since its synchronous the onsubmit script executes without waiting for the response from Script include

In you case you need to write synchronous GlideAjax so that it until response comes your remaining script doesnt executes.

 

Examples of synchronous GlideAjax

 

Regards,

Harish Murikinati.