You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: components/form/validation.md
+99-29Lines changed: 99 additions & 29 deletions
Original file line number
Diff line number
Diff line change
@@ -133,7 +133,6 @@ You can use the built-in `DataAnnotationsValidator` that comes with the Blazor f
133
133
</FormValidation>
134
134
</TelerikForm>
135
135
136
-
137
136
@code {
138
137
public Person person { get; set; } = new Person();
139
138
@@ -158,8 +157,7 @@ You can use the <a href="https://learn.microsoft.com/en-us/aspnet/core/blazor/fo
158
157
159
158
When using a model with nested objects and fields, specify their `Field` settings as a dot-separate string, do *not* use the `nameof` operator, it does not return the full name of the model.
160
159
161
-
<divclass="skip-repl"></div>
162
-
````RAZOR
160
+
````RAZOR.skip-repl
163
161
@using System.Dynamic
164
162
@using System.ComponentModel.DataAnnotations
165
163
@@ -228,50 +226,122 @@ When using a model with nested objects and fields, specify their `Field` setting
228
226
229
227
### Fluent Validation
230
228
231
-
You can use third-party validation libraries that integrate with the standard `EditContext` such as <ahref="https://fluentvalidation.net/"target="_blank">FluentValidation</a> together with the Telerik Form for Blazor.
229
+
You can use third-party validation libraries that integrate with the standard `EditContext` such as [FluentValidation](https://fluentvalidation.net/) together with the Telerik Form for Blazor.
232
230
233
-
>note Such third party tools are not included with the Telerik UI for Blazor package. Your project must reference their NuGet packages explicitly. The code snippet below will not run unless you install the an appropriate package first. You can find some in <ahref="https://docs.fluentvalidation.net/en/latest/blazor.html"target="_blank">their official documentation</a>.
231
+
The example below:
234
232
233
+
* Requires the [`Blazored.FluentValidation` NuGet package](https://www.nuget.org/packages/Blazored.FluentValidation). Also refer to the [FluentValidation documentation](https://docs.fluentvalidation.net/en/latest/blazor.html).
234
+
* Shows how to pass `ValueExpression` from the parent component to custom child components in a [Form item template](slug:form-formitems-template) or a [Grid editor template](slug:grid-templates-editor). If the `ValueExpression` is not passed correctly, the app will throw [exception similar to: `Cannot validate instances of type 'ComponentName'. This validator can only validate instances of type 'ModelClassName'`](slug:form-kb-fluent-validation-cannot-validate-instances-of-type).
title: FluentValidation Exception Cannot Validate Instances of Type 'ComponentName'
3
+
description: Learn how to troubleshoot and resolve runtime exceptions Cannot Validate Instances of Type 'ComponentName'. This Validator Can only Validate Instances of Type 'ModelClassName' in Blazor apps.
4
+
type: troubleshooting
5
+
page_title: FluentValidation Exception Cannot Validate Instances of Type 'ComponentName'. This Validator Can only Validate Instances of Type 'ModelClassName'
The KB article deals with the following scenarios that throw a runtime exception. It explains what causes the error and how to fix it.
29
+
30
+
* A Telerik Form is using a `FluentValidation` validator. A custom input component in a [`FormItem``Template`](slug:form-formitems-template) crashes the page on value edit.
31
+
* An Telerik Grid with inline, incell or popup `EditMode` is using `<FluentValidationValidator>` for validation. One of the Grid columns has an `<EditorTemplate>` with a custom component that wraps a `<TelerikTextBox>`.
32
+
33
+
In both cases the exception message that occurs on value edit in the custom component is:
34
+
35
+
`Cannot validate instances of type 'ComponentName'. This validator can only validate instances of type 'ModelClassName'`
36
+
37
+
In general, the exception may occur when using `FluentValidation` with inputs that are wrapped in custom child components.
38
+
39
+
## Cause
40
+
41
+
The exception occurs, because the custom child component that holds the input is not receiving the correct `ValueExpression` from the parent component that holds the edit form.
42
+
43
+
The issue is not related to or caused by Telerik UI for Blazor. The same behavior can occur with a standard Blazor `EditForm` and `InputText` components.
44
+
45
+
## Solution
46
+
47
+
1. Define a public parameter of type `Expression<System.Func<T>>`, which receives the correct expression from the parent component. `T` is the value type, which the custom child component is editing. The parameter name must be consistent with the other two related parameter names that deal the two-way value binding:
48
+
*`Foo`
49
+
*`FooChanged`
50
+
*`FooExpression`
51
+
1. Pass the validation expression from the parent to the child component. There are two possible options:
52
+
* Use two-way binding for the value parameter (`@bind-Foo`) in the parent component.
53
+
* Pass the expression explicitly by setting `FooExpression` in the parent component.
54
+
55
+
For a full runnable example, refer to [Form Fluent Validation](slug:form-validation#fluent-validation).
56
+
57
+
>caption Using ValueExpression for validation in child components
58
+
59
+
```RAZOR TextBox.razor
60
+
@using System.Linq.Expressions
61
+
62
+
<TelerikTextBox Value="@Value"
63
+
ValueChanged="@ValueChanged"
64
+
ValueExpression="@ValueExpression" />
65
+
66
+
@code {
67
+
[Parameter]
68
+
public string Value { get; set; } = string.Empty;
69
+
70
+
[Parameter]
71
+
public EventCallback<string> ValueChanged { get; set; }
72
+
73
+
[Parameter]
74
+
public Expression<System.Func<string>>? ValueExpression { get; set; }
0 commit comments