0

I'm using jqGrid, latest version, and when I apply a edit rule that is a custom function and perform ajax it always returns "Custom function should always return a array". I think it's a timing issue so I set the ajax to false but still have the problem. Anyone out there have a custom function that performs a ajax call which works correctly. Appreciate any help. Thank you.

jQuery(softwareReportingGrid.gridId).jqGrid({
    editurl: 'clientArray',
    datatype: 'json',
    colNames: ["Car"],
    colModel: [
        {"index":"Car","name":"Car","edittype":"text","editable":true,
            "editrules":{"custom":true,"custom_func":validateCar,"required":true}}
    ....

I have the following javascript function which is called

validateCar: function (value, colname) {
    jQuery.ajax({
        async: false,
        url: validateCarUrl,
        data: { carName: value },
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            if (data) {
                return [true, '']
            } else {
                return [false, value + ' is not a valid car'];
            }
        },
        error: function () { alert('Error trying to validate car ' + value); }
    });
} 
Oleg
  • 220,925
  • 34
  • 403
  • 798
Two Seeds
  • 53
  • 1
  • 5

1 Answers1

5

Your validateCar() does not return anything because AJAX is asynchronous. And even if it was, you are returning something from a function assigned as a success handler, not from the outer validateCar() function.

When the response from your $.ajax arrives, the method returned long ago. You either have to use synchronous AJAX (kind of discouraged):

validateCar: function (value, colname) {
    var result = null;
    jQuery.ajax({
        async: false,      //this is crucial
        url: validateCarUrl,
        data: { carName: value },
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            if (data) {
                result = [true, '']
            } else {
                result = [false, value + ' is not a valid car'];
            }
        },
        error: function () { alert('Error trying to validate car ' + value); }
    });
    return result;
} 

or redesign your function.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • Thank you for your fast response. – Two Seeds Feb 28 '12 at 21:48
  • 1
    @TwoSeeds: You should use your right to vote up helpful answers, like I just did myself. You have right to vote about 30 questions or answers **per day** (see [here](http://meta.stackexchange.com/a/5213/147495)). If you would use the right you will help another visitors of the page and you will help also the searching engine. – Oleg Feb 28 '12 at 22:01
  • I fixed small typing error in the second `result = [` (it was just `result [`). With `ajax: false` you mean probably `cache: false`? – Oleg Feb 28 '12 at 22:27
  • @Oleg: thanks for correcting it! I see that OP now changed `ajax: false` to `async: false` which makes half of my answer pointless... – Tomasz Nurkiewicz Feb 28 '12 at 22:32
  • Yes, I agree, but the unknown `ajax: false` option exist now only in your code. On the other side the usage of `cache: false` can be helpful in case of HTTP GET (default `type: "GET"`) especially for Internet Explorer. So I think it would be better either to remove unknown `ajax: false` or to fix to something like `cache: false`. You should decide yourself what should be in the code from your answer. :-) – Oleg Feb 28 '12 at 22:47