0

I used ajax call in the custom function of jQgrid validation,

this is my function

function customFuction(value1, colname) {
    var result = null;
    $.ajax({
        url: "../ajax/check_269.php",
        type: "POST",
        data: {
            val: value1,
            col: colname,
            parentId: row_id
        },
        dataType: "html",
        async: false,
        success: function (data) {
            if (data == "error") {
                result[false, value1 + "no se permite"];
            } else {
                result[true, value1 + "Success"];
            }
        },
        error: function () {
            alert('Error trying to validate car ');
        }
    });
    return result;
}    

it give me error like this

TypeError: result is null
palaѕн
  • 72,112
  • 17
  • 116
  • 136
Nisarg
  • 3,024
  • 5
  • 32
  • 54
  • what will be the method name in check_269.php? – Dhaval Marthak Jun 28 '13 at 06:22
  • simple query in check_269.php based on data sent by this ajax – Nisarg Jun 28 '13 at 06:26
  • 1
    ajax is asynchronus... your result will be returned while the ajax call is taking place and does not wait for the ajax function to complete.. – bipen Jun 28 '13 at 06:30
  • that's right, also i can't understand why are you using statement like result[boolean, string] – vladkras Jun 28 '13 at 06:35
  • after assuming result as null? null is an empty object in JavaScript, so you'd better use array or (if you do really need an object) result.status and result.msg or smth like this – vladkras Jun 28 '13 at 06:42

2 Answers2

0

First of all the variable success are not changed inside of success callback. You should change the line

result[true, value1 + "Success"];

to

result = [true, value1 + "Success"];

and modify the line result[false, value1 + "no se permite"]; in the same way.

The main problem is that one don't need any server side validation function. If the data will be saved the server should make validation of input data. If the data are wrong the server should return some error description and some error HTTP code. If you use form editing for example then you can specify errorTextFormat callback to decode the server response in case of error. Per default jqGrid will interpret the body of the error server response as HTML fragment which will be displayed to the server.

Oleg
  • 220,925
  • 34
  • 403
  • 798
0

ajax is asynchronus.. the reason you are getting null is because bythetime ajax call is completed the function will return the result as null and doesn't wait for ajax call to complete..

one way around is to return the result inside the success callback function of ajax to make sure ajax call completes.

 function customFuction(value1, colname) {
var result = null;
$.ajax({
    url: "../ajax/check_269.php",
    type: "POST",
    data: {
        val: value1,
        col: colname,
        parentId: row_id
    },
    dataType: "html",
    async: false,
    success: function (data) {
        if (data == "error") {
            result = [false, value1 + "no se permite"];
        } else {
            result = [true, value1 + "Success"];
        }
         return result;
    },
    error: function () {
        alert('Error trying to validate car ');
    }
});

}    

and i guess you missed = to set the result array.

bipen
  • 36,319
  • 9
  • 49
  • 62