Skip to content

Commit bad0d13

Browse files
svdimitrdimodi
andauthored
Validation in Grid and TreeList docs (#614)
* feat(validation): add validation docs for the Grid * feat(validation): add validation docs for the TreeList * chore(grid): wording * Update components/grid/editing/validation.md Co-authored-by: Dimo Dimov <[email protected]> * Update components/grid/editing/validation.md Co-authored-by: Dimo Dimov <[email protected]> * Update components/grid/editing/validation.md Co-authored-by: Dimo Dimov <[email protected]> * Update components/grid/editing/validation.md Co-authored-by: Dimo Dimov <[email protected]> * Update components/grid/editing/validation.md Co-authored-by: Dimo Dimov <[email protected]> * Update components/grid/editing/validation.md Co-authored-by: Dimo Dimov <[email protected]> * Update components/grid/editing/validation.md Co-authored-by: Dimo Dimov <[email protected]> * Update components/treelist/editing/validation.md Co-authored-by: Dimo Dimov <[email protected]> * Update components/treelist/editing/validation.md Co-authored-by: Dimo Dimov <[email protected]> * Update components/treelist/editing/validation.md Co-authored-by: Dimo Dimov <[email protected]> * Update components/grid/editing/validation.md Co-authored-by: Dimo Dimov <[email protected]> * Update components/treelist/editing/validation.md Co-authored-by: Dimo Dimov <[email protected]> * Update components/treelist/editing/validation.md Co-authored-by: Dimo Dimov <[email protected]> * Update components/treelist/editing/validation.md Co-authored-by: Dimo Dimov <[email protected]> * Update components/treelist/editing/validation.md Co-authored-by: Dimo Dimov <[email protected]> * Update components/treelist/editing/validation.md Co-authored-by: Dimo Dimov <[email protected]> * fix(slugs): fix slugs in validation articles Co-authored-by: Dimo Dimov <[email protected]>
1 parent bdfc2cd commit bad0d13

File tree

2 files changed

+492
-0
lines changed

2 files changed

+492
-0
lines changed

components/grid/editing/validation.md

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
---
2+
title: Validation
3+
page_title: Grid Validation
4+
description: Built-in validation in the Grid and how to customize the validation behavior.
5+
slug: grid-editing-validation
6+
tags: telerik,blazor,grid,validation,editing
7+
published: True
8+
position: 40
9+
---
10+
11+
# Grid Validation
12+
13+
The Telerik UI for Blazor Grid supports built-in validation that is enabled by default. The Grid passes an `EditContext` as a cascading value to the editable cells. If any validation messages are present, the Grid will render them as [Validation Tooltips]({%slug validation-tools-tooltip%}) on hover of the specific editor.
14+
15+
#### In this Article:
16+
17+
* [Disable the validation](#disable-the-validation)
18+
* [Use a custom validator](#use-custom-validator)
19+
20+
## Disable Validation
21+
22+
To disable the built-in validation, add a `<GridValidationSettings>` tag to the `<GridSettings>` and set the `Enabled` parameter to `false`.
23+
24+
````CSHTML
25+
@* Disable the built-in validation in the Grid *@
26+
27+
@using System.ComponentModel.DataAnnotations @* for the validation attributes *@
28+
29+
<TelerikGrid Data=@MyData EditMode="@GridEditMode.Inline" Pageable="true" Height="500px"
30+
OnUpdate="@UpdateHandler" OnEdit="@EditHandler" OnDelete="@DeleteHandler" OnCreate="@CreateHandler" OnCancel="@CancelHandler">
31+
<GridSettings>
32+
<GridValidationSettings Enabled="false">
33+
</GridValidationSettings>
34+
</GridSettings>
35+
<GridToolBar>
36+
<GridCommandButton Command="Add" Icon="add">Add Employee</GridCommandButton>
37+
</GridToolBar>
38+
<GridColumns>
39+
<GridColumn Field=@nameof(SampleData.ID) Title="ID" Editable="false" />
40+
<GridColumn Field=@nameof(SampleData.Name) Title="Name" />
41+
<GridCommandColumn>
42+
<GridCommandButton Command="Save" Icon="save" ShowInEdit="true">Update</GridCommandButton>
43+
<GridCommandButton Command="Edit" Icon="edit">Edit</GridCommandButton>
44+
<GridCommandButton Command="Delete" Icon="delete">Delete</GridCommandButton>
45+
<GridCommandButton Command="Cancel" Icon="cancel" ShowInEdit="true">Cancel</GridCommandButton>
46+
</GridCommandColumn>
47+
</GridColumns>
48+
</TelerikGrid>
49+
50+
@code {
51+
void EditHandler(GridCommandEventArgs args)
52+
{
53+
SampleData item = (SampleData)args.Item;
54+
55+
// prevent opening for edit based on condition
56+
if (item.ID < 3)
57+
{
58+
args.IsCancelled = true;// the general approach for cancelling an event
59+
}
60+
61+
Console.WriteLine("Edit event is fired.");
62+
}
63+
64+
async Task UpdateHandler(GridCommandEventArgs args)
65+
{
66+
SampleData item = (SampleData)args.Item;
67+
68+
// perform actual data source operations here through your service
69+
await MyService.Update(item);
70+
71+
// update the local view-model data with the service data
72+
await GetGridData();
73+
74+
Console.WriteLine("Update event is fired.");
75+
}
76+
77+
async Task DeleteHandler(GridCommandEventArgs args)
78+
{
79+
SampleData item = (SampleData)args.Item;
80+
81+
// perform actual data source operation here through your service
82+
await MyService.Delete(item);
83+
84+
// update the local view-model data with the service data
85+
await GetGridData();
86+
87+
Console.WriteLine("Delete event is fired.");
88+
}
89+
90+
async Task CreateHandler(GridCommandEventArgs args)
91+
{
92+
SampleData item = (SampleData)args.Item;
93+
94+
// perform actual data source operation here through your service
95+
await MyService.Create(item);
96+
97+
// update the local view-model data with the service data
98+
await GetGridData();
99+
100+
Console.WriteLine("Create event is fired.");
101+
}
102+
103+
async Task CancelHandler(GridCommandEventArgs args)
104+
{
105+
SampleData item = (SampleData)args.Item;
106+
107+
// if necessary, perform actual data source operation here through your service
108+
109+
Console.WriteLine("Cancel event is fired.");
110+
}
111+
112+
113+
// in a real case, keep the models in dedicated locations, this is just an easy to copy and see example
114+
public class SampleData
115+
{
116+
public int ID { get; set; }
117+
[Required]
118+
public string Name { get; set; }
119+
}
120+
121+
public List<SampleData> MyData { get; set; }
122+
123+
async Task GetGridData()
124+
{
125+
MyData = await MyService.Read();
126+
}
127+
128+
protected override async Task OnInitializedAsync()
129+
{
130+
await GetGridData();
131+
}
132+
133+
// the following static class mimics an actual data service that handles the actual data source
134+
// replace it with your actual service through the DI, this only mimics how the API can look like and works for this standalone page
135+
public static class MyService
136+
{
137+
private static List<SampleData> _data { get; set; } = new List<SampleData>();
138+
139+
public static async Task Create(SampleData itemToInsert)
140+
{
141+
itemToInsert.ID = _data.Count + 1;
142+
_data.Insert(0, itemToInsert);
143+
}
144+
145+
public static async Task<List<SampleData>> Read()
146+
{
147+
if (_data.Count < 1)
148+
{
149+
for (int i = 1; i < 50; i++)
150+
{
151+
_data.Add(new SampleData()
152+
{
153+
ID = i,
154+
Name = "Name " + i.ToString()
155+
});
156+
}
157+
}
158+
159+
return await Task.FromResult(_data);
160+
}
161+
162+
public static async Task Update(SampleData itemToUpdate)
163+
{
164+
var index = _data.FindIndex(i => i.ID == itemToUpdate.ID);
165+
if (index != -1)
166+
{
167+
_data[index] = itemToUpdate;
168+
}
169+
}
170+
171+
public static async Task Delete(SampleData itemToDelete)
172+
{
173+
_data.Remove(itemToDelete);
174+
}
175+
}
176+
}
177+
````
178+
179+
## Use a Custom Validator
180+
181+
You can validate the Grid with any validator that uses the `EditContext`. To change the default validator, add a `<ValidatorTemplate>` tag in `<GridSettings>`. Define the custom validator in the `ValidatorTemplate`.
182+
183+
## See Also
184+
185+
* [Grid Editing]({%slug components/grid/editing/overview%})
186+

0 commit comments

Comments
 (0)