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); }
});
}
Your
validateCar()does not return anything because AJAX is asynchronous. And even if it was, you are returning something from a function assigned as asuccesshandler, not from the outervalidateCar()function.When the response from your
$.ajaxarrives, the method returned long ago. You either have to use synchronous AJAX (kind of discouraged):or redesign your function.