Skip to content

Commit 953075f

Browse files
committed
docs: Add Custom validation in Grid filter menus grid how-to
1 parent 5353066 commit 953075f

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
---
2+
title: Use custom validation in Grid Filter menus
3+
page_title: Use custom validation in Grid Filter menus | Kendo UI Grid Widget
4+
description: "Learn how to Use custom validation in Grid Filter menus when filtering is applied."
5+
slug: howto_gridfiltering_custom_validation_filter_menu_grid
6+
---
7+
8+
# Use custom validation in Grid Filter menus
9+
10+
The example below demonstrates how to use custom validation in Grid Filter menus
11+
12+
This approach comes in handy when you want to validate the user input in the Grid-generated filtering UI in the Filter menu (e.g. Kendo UI DatePicker for date fields or Kendo UI NumericTextBox for numeric fields).
13+
14+
The example below follows these steps:
15+
16+
1. Handle the [`filterMenuInit`](/api/javascript/ui/grid#events-filterMenuInit) event and get a reference to the corresponding widgets
17+
2. Get a reference to the built-in Kendo UI Validator and use its [`rules`](/api/javascript/ui/validator#configuration-rules) and [`messages`](/api/javascript/ui/validator#configuration-messages) options to add the necessary custom validation logic and messages
18+
19+
As a result, the custom validation will be triggered when the user input does not obey the predefined rules.
20+
21+
###### Example
22+
23+
```html
24+
<div id="grid"></div>
25+
<script>
26+
$("#grid").kendoGrid({
27+
columns: [
28+
{ field: "name" },
29+
{ field: "date", format: '{0:MM/dd/yyyy}' },
30+
{ field: "age" }
31+
],
32+
dataSource: {
33+
data: [
34+
{ name: "Jane Doe", date: new Date(2016, 10, 15), age: 25},
35+
{ name: "John Doe", date: new Date(2016, 10, 17), age: 20}
36+
],
37+
schema: {
38+
model: {
39+
fields: {
40+
date: {
41+
type: 'date'
42+
},
43+
age: {
44+
type: 'number'
45+
}
46+
}
47+
}
48+
}
49+
},
50+
filterable: true,
51+
filterMenuInit: function(e) {
52+
if (e.field === "date" || e.field === 'age') {
53+
e.container.find("[data-role=datepicker]").each(function(idx) {
54+
$(this).attr("name", "date" + idx);
55+
56+
$('<span class="k-invalid-msg" data-for="' + "date" + idx + '"></span>')
57+
.insertAfter($(this).closest(".k-datepicker"));
58+
});
59+
60+
e.container.find("[data-role=numerictextbox]").each(function(idx) {
61+
$(this).attr("name", "date" + idx);
62+
63+
$('<span class="k-invalid-msg" data-for="' + "date" + idx + '"></span>')
64+
.insertAfter($(this).closest(".k-numerictextbox"));
65+
});
66+
67+
e.container.kendoValidator({
68+
rules: {
69+
datePickers: function(input){
70+
var dtp = input.data('kendoDatePicker');
71+
if(dtp && input.val()){
72+
return !!dtp.value() ;
73+
}
74+
75+
return true;
76+
},
77+
numerics: function(input){
78+
var ntb = input.data('kendoNumericTextBox');
79+
if(ntb && input.val()){
80+
return !!(ntb.value() > 0);
81+
}
82+
83+
return true;
84+
}
85+
},
86+
change: function() {
87+
var button = e.container.find(".k-primary");
88+
this.value() ? button.removeAttr("disabled") : button.prop("disabled", "disabled")
89+
},
90+
messages: {
91+
datePickers: "Please enter a valid date",
92+
numerics: "Age must be > 0"
93+
}
94+
});
95+
}
96+
}
97+
});
98+
</script>
99+
```
100+
101+
## See Also
102+
103+
Other articles on the Kendo UI Grid and how-to examples:
104+
105+
* [JavaScript API Reference](/api/javascript/ui/grid)
106+
* [How to Add Cascading DropDownList Editors]({% slug howto_add_cascading_dropdown_list_editors_grid %})
107+
* [How to Copy Data from Excel]({% slug howto_copy_datafrom_excel_grid %})
108+
* [How to Drag and Drop Rows between Grids]({% slug howto_dragand_drop_rows_between_twogrids_grid %})
109+
* [How to Enable ForeignKey Column Sorting by Text]({% slug howto_enable_foreignkey_sotringby_text_grid %})
110+
* [How to Implement Stable Sort in Chrome]({% slug howto_implement_stable_sortin_chrome_grid %})
111+
* [How to Initialize Data Attribute with Detail Template]({% slug howto_initialize_data_attributewith_detail_template_grid %})
112+
* [How to Load and Append More Records While Scrolling Down]({% slug howto_loadand_append_morerecords_while_scrollingdown_grid %})
113+
* [How to Perform CRUD Operations with Local Storage Data]({% slug howto_perform_crud_operationswith_local_storage_data_grid %})
114+
* [How to Persist Expanded Rows after Refresh]({% slug howto_persist_expanded_rows_afetrrefresh_grid %})
115+
* [How to Set Cell Color Based on ForeignKey Values]({% slug howto_set_cell_color_basedon_foreignkey_values_grid %})
116+
* [How to Show Tooltip for Column Records]({% slug howto_show_tooltipfor_column_records_grid %})
117+
* [How to Update Toolbar Content Using MVVM Binding]({% slug howto_update_toolbar_content_using_mvvmbinding_grid %})
118+
119+
For more runnable examples on the Kendo UI Grid, browse its [**How To** documentation folder]({% slug howto_create_custom_editors_grid %}).

0 commit comments

Comments
 (0)