Search code examples
postkendo-uikendo-gridpreventdefault

kendo ui grid prevent posting data on client side validation error


I have done some clientside validation.

 parameterMap: function(options, operation) {
            if (operation !== "read") {
                var valid = customValidation(options);
                console.log(valid);
                if (valid) {
                    return JSON.stringify({ discountPromotionViewModel: options });
                }
            }
        },

And if the customValidation method returns false i don't want the grid to post the data. Now it posts an empty model so it still reaches my controller. How do i prevent it from even posting the data?


Solution

  • Do your validation inside Grid's event save, then if it's invalid result prevent it with e.preventDefault function. It will prevent POST request to server.

    $("#grid").kendoGrid({
        // some grid configuration 
        save: function(e) {
            var model = e.model,
                valid = customValidation(model);
            if(!valid) e.preventDefault();
        }
    });