Skip to content

Commit 3040a0d

Browse files
committed
Sync with Kendo UI Professional
1 parent 162b31d commit 3040a0d

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
---
2+
title: Triggering Validation Programmatically for Kendo UI for jQuery Grid
3+
description: Learn how to programmatically validate all rows of a Kendo UI for jQuery Grid and display validation messages for invalid cells.
4+
type: how-to
5+
page_title: Programmatically Validate Rows in Kendo UI for jQuery Grid
6+
meta_title: Programmatically Validate Rows in Kendo UI for jQuery Grid
7+
slug: programmatically-validate-rows-kendo-ui-jquery-grid
8+
tags: kendo-ui-for-jquery, grid, validation, data-source
9+
res_type: kb
10+
ticketid: 1688346
11+
---
12+
13+
## Environment
14+
15+
<table>
16+
<tbody>
17+
<tr>
18+
<td> Product </td>
19+
<td> Kendo UI for jQuery Grid </td>
20+
</tr>
21+
<tr>
22+
<td> Version </td>
23+
<td> Current </td>
24+
</tr>
25+
</tbody>
26+
</table>
27+
28+
## Description
29+
30+
I need to trigger validation programmatically for all rows in the [Kendo UI for jQuery Grid](https://docs.telerik.com/kendo-ui/controls/data-management/grid/overview) and display validation messages on cells with invalid data. The validation occurs when data is loaded from an external source, and the invalid cells should be highlighted similarly to how the Grid's built-in validation mechanism works.
31+
32+
This knowledge base article also answers the following questions:
33+
- How can I validate all rows in Kendo UI for jQuery Grid programmatically?
34+
- How can I highlight invalid cells in Kendo UI for jQuery Grid?
35+
- How can I show validation messages for invalid data in Kendo UI for jQuery Grid?
36+
37+
## Solution
38+
39+
Use a custom approach to validate all data items in the Kendo UI for jQuery Grid's DataSource. Highlight invalid cells and display validation messages manually. Follow these steps:
40+
41+
### Steps
42+
1. Configure the Kendo UI Grid with a DataSource and schema defining the fields.
43+
2. Add a button to trigger validation.
44+
3. Use JavaScript to loop through the DataSource items and validate each field manually.
45+
4. Highlight invalid cells using CSS classes and append validation messages.
46+
47+
Example implementation:
48+
49+
```dojo
50+
<div id="grid"></div>
51+
<button id="validateBtn">Validate Grid</button>
52+
53+
<script>
54+
$(document).ready(function () {
55+
var data = [
56+
{ Name: "John", Age: 30 },
57+
{ Name: "", Age: 25 },
58+
{ Name: "Alice", Age: -5 }
59+
];
60+
61+
$("#grid").kendoGrid({
62+
dataSource: {
63+
data: data,
64+
schema: {
65+
model: {
66+
fields: {
67+
Name: { type: "string" },
68+
Age: { type: "number" }
69+
}
70+
}
71+
}
72+
},
73+
editable: true,
74+
columns: [
75+
{ field: "Name", title: "Name" },
76+
{ field: "Age", title: "Age" }
77+
]
78+
});
79+
80+
$("#validateBtn").click(function () {
81+
var grid = $("#grid").data("kendoGrid");
82+
var data = grid.dataSource.view();
83+
84+
// Clear previous validation messages and styles
85+
grid.tbody.find("td").removeClass("k-invalid");
86+
grid.tbody.find(".validation-message").remove();
87+
88+
data.forEach(function (item, rowIndex) {
89+
var row = grid.tbody.find("tr:eq(" + rowIndex + ")");
90+
91+
// Validate Name
92+
if (!item.Name || item.Name.trim() === "") {
93+
var nameCell = row.find("td:eq(0)");
94+
nameCell.addClass("k-invalid");
95+
nameCell.append('<div class="k-tooltip k-tooltip-error k-validator-tooltip k-invalid-msg" data-for="Name" id="Name-error"><span class="k-tooltip-content">Name is required.</span><span class="k-callout k-callout-n"></span></div>');
96+
}
97+
98+
// Validate Age
99+
if (typeof item.Age !== "number" || item.Age <= 0) {
100+
var ageCell = row.find("td:eq(1)");
101+
ageCell.addClass("k-invalid");
102+
ageCell.append('<div class="validation-message">Age must be greater than 0.</div>');
103+
}
104+
});
105+
});
106+
});
107+
</script>
108+
109+
<style>
110+
.k-invalid {
111+
border: 1px solid red !important;
112+
position: relative;
113+
}
114+
115+
.validation-message {
116+
color: red;
117+
font-size: 12px;
118+
margin-top: 4px;
119+
}
120+
</style>
121+
```
122+
123+
### Key Points:
124+
- Use the [`dataSource.view()`](/api/javascript/data/datasource/methods/view) method to fetch the current data items.
125+
- Highlight invalid cells using the `k-invalid` CSS class or any custom class.
126+
- Append validation messages as HTML elements inside the cells.
127+
128+
## See Also
129+
130+
- [Kendo UI for jQuery Grid Overview](https://docs.telerik.com/kendo-ui/controls/data-management/grid/overview)
131+
- [Kendo UI for jQuery DataSource](https://docs.telerik.com/kendo-ui/framework/datasource/overview)

0 commit comments

Comments
 (0)