Kendo Custom Validation Rules in a Data Grid
Why doesn't returning false in the custom rule display the error message?
by David Salahi
HTML
<link rel="stylesheet" href="http://cdn.kendostatic.com/2013.3.1324/styles/kendo.common.min.css">
<link rel="stylesheet" href="http://cdn.kendostatic.com/2013.3.1324/styles/kendo.silver.min.css">
<link rel="stylesheet" href="http://demos.telerik.com/kendo-ui/content/shared/styles/examples.css">
<link rel="stylesheet" href="http://cdn.kendostatic.com/2013.3.1324/styles/kendo.rtl.min.css">
<script src="http://cdn.kendostatic.com/2013.3.1324/js/kendo.all.min.js"></script>
<div id="example" class="k-content">
<div id="grid"></div>
</div>
JavaScript
var validatorRules = {
rules: {
// This rule is executed when leaving a cell but the return value of false doesn't display any error message or prevent leaving the cell
customRule1: function (input) {
// OpCode must not be empty
if (input.attr("name") == "ProductName") {
return $.trim(input.val()) !== "";
}
}
},
messages: {
customRule1: "All fields are required"
}
};
var crudServiceBaseUrl = "http://demos.telerik.com/kendo-ui/service",
dataSource = new kendo.data.DataSource({
transport: {
read: {
url: crudServiceBaseUrl + "/Products",
dataType: "jsonp"
},
parameterMap: function (options, operation) {
if (operation !== "read" && options.models) {
return {
models: kendo.stringify(options.models)
};
}
}
},
batch: true,
pageSize: 20,
schema: {
model: {
id: "ProductID",
fields: {
ProductID: {
editable: false,
nullable: true
},
ProductName: {},
UnitPrice: {
type: "number",
validation: {
required: true,
min: 1
}
}
}
}
}
});
$("#grid").kendoGrid({
dataSource: dataSource,
pageable: true,
height: 430,
toolbar: ["create"],
columns: [{
field: "ProductName",
width: "150px"
}, {
field: "UnitPrice",
title: "Unit Price",
format: "{0:c}",
width: "100px"
}, ],
...