Skip to content

Commit 00c8507

Browse files
authored
docs(Validation): Revamp documentation and enhance ValueExpression syntax information (#3228)
* kb(Validation): Enhance ValueExpression syntax information * docs(Validation): Revamp ValidationMessage documentation * docs(Validation): Revamp Validation components documentation and examples
1 parent 3b99e6e commit 00c8507

File tree

5 files changed

+407
-531
lines changed

5 files changed

+407
-531
lines changed

components/validation/message.md

Lines changed: 124 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -10,166 +10,182 @@ position: 15
1010

1111
# Telerik Validation Message for Blazor
1212

13-
The <a href = "https://www.telerik.com/blazor-ui/validation-message" target="_blank">Telerik Validation Message for Blazor</a> adds customization options on top of the standard <a href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.forms.validationmessage-1" target="_blank">.NET ValidationMessage</a>, such as [`Template`](#template) and [`Class`](#class) parameters.
13+
The [Telerik Validation Message for Blazor](https://www.telerik.com/blazor-ui/validation-message) adds built-in styling and customization options on top of the standard [.NET ValidationMessage](https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.forms.validationmessage-1), such as [`Template`](#template) and [`Class`](#class) parameters.
1414

15-
## Using Validation Message with TelerikForm
15+
## Basics
1616

17-
To enable Telerik Validation Messages for a form field:
17+
To use a Telerik Validation Message:
1818

19-
1. Add a `<TelerikValidationMessage>` tag near the respective `<FormItem>` tag, or [inside a form item `<Template>`](slug:form-formitems-template).
20-
1. Provide a lambda expression in the `For` parameter that sets the associated property of the model, just like with the standard Blazor `ValidationMessage` component.
21-
1. (optional) Disable the built-in validation messages of the Telerik Form to avoid repetition. Set `ValidationMessageType="@FormValidationMessageType.None"`.
19+
1. Add the `<TelerikValidationMessage>` tag.
20+
1. Set the `For` parameter in the same way as with a standard Blazor `<ValidationMessage>`. There are two options:
21+
* If the `TelerikValidationMessage` is in the same component as the Form or if the Form model object is available, use a lambda expression that points to a property of the Form model.
22+
````RAZOR.skip-repl
23+
<TelerikValidationMessage For="@(() => Customer.FirstName)" />
2224
23-
>caption Use Telerik Validation Message in a TelerikForm
25+
@code {
26+
private CustomerModel Customer { get; set; } = new();
27+
28+
public class CustomerModel
29+
{
30+
public string FirstName { get; set; } = string.Empty;
31+
}
32+
}
33+
````
34+
* If the [validation message is in a child component](slug:inputs-kb-validate-child-component) that receives a `ValueExpression`, set the `For` parameter directly to the expression, without a lambda.
35+
````RAZOR.skip-repl
36+
<TelerikValidationMessage For="@FirstNameExpression" />
37+
38+
@code {
39+
[Parameter]
40+
public Expression<System.Func<string>>? FirstNameExpression { get; set; }
41+
}
42+
````
43+
44+
Refer to the following sections for additional information and examples with the [Telerik Form](#using-with-telerikform) and standard [Blazor `<EditForm>`](#using-with-editform).
45+
46+
## Using with TelerikForm
47+
48+
The Telerik Form [displays inline validation messages by default if validation is enabled](slug:form-validation). You may need to define `<TelerikValidationMessage>` components manually when you want to:
49+
50+
* Use [form item templates](slug:form-formitems-template). In this case, [add the validation message in the form item template](slug:form-formitems-template#example).
51+
* Customize the validation messages, for example, change their rendering with a [validation message template](#template). In this case, add the validation message inside a [Form item template](slug:form-formitems-template#example).
52+
* Customize the placement of the validation messages in the Form, so that they are outside the Form item containers. In this case, consider a [`<FormItemsTemplate>`](slug:form-formitems-formitemstemplate) that gives you full control over the Form rendering between the form items. Alternatively, consider a [Telerik ValidationSummary](slug:validation-tools-summary).
53+
54+
>caption Use Telerik ValidationMessage in a TelerikForm
2455
2556
````RAZOR
2657
@using System.ComponentModel.DataAnnotations
2758
28-
<TelerikForm Model="@customer" Width="600px"
29-
ValidationMessageType="@FormValidationMessageType.None">
59+
<TelerikForm Model="@Employee"
60+
Width="300px">
3061
<FormValidation>
3162
<DataAnnotationsValidator />
3263
</FormValidation>
33-
3464
<FormItems>
35-
<FormItem Field="@nameof(Customer.CustomerName)" LabelText="Name" />
36-
<TelerikValidationMessage For="@(() => customer.CustomerName)" />
37-
38-
<FormItem Field="@nameof(Customer.CustomerAge)" LabelText="Age" />
39-
<TelerikValidationMessage For="@(() => customer.CustomerAge)" />
40-
41-
<FormItem Field="@nameof(Customer.EmailAddress)" LabelText="Email Address" />
42-
<TelerikValidationMessage For="@(() => customer.EmailAddress)" />
65+
<FormItem Field="@nameof(Person.FirstName)" LabelText="First Name">
66+
<Template>
67+
<label for="first-name" class="k-label k-form-label">First Name</label>
68+
<div class="k-form-field-wrap">
69+
<TelerikTextBox @bind-Value="@Employee.FirstName"
70+
Id="first-name" />
71+
<TelerikValidationMessage For="@(() => Employee.FirstName)" />
72+
</div>
73+
</Template>
74+
</FormItem>
75+
<FormItem Field="@nameof(Person.LastName)" LabelText="Last Name" />
4376
</FormItems>
4477
</TelerikForm>
4578
4679
@code {
47-
private Customer customer = new Customer();
80+
private Person Employee { get; set; } = new();
4881
49-
public class Customer
82+
public class Person
5083
{
51-
[Required(ErrorMessage = "Please enter your name")]
52-
[MaxLength(40, ErrorMessage = "The name must be up to 40 characters long")]
53-
public string CustomerName { get; set; }
54-
55-
[Required(ErrorMessage = "Please enter your age")]
56-
[Range(18, 120, ErrorMessage = "You should be at least 18 years old to place an order")]
57-
public int CustomerAge { get; set; }
84+
[Required(ErrorMessage = "Please enter a first name")]
85+
[MinLength(2, ErrorMessage = "The first name must be at least 2 characters long")]
86+
[MaxLength(40, ErrorMessage = "The first name must be up to 40 characters long")]
87+
public string FirstName { get; set; } = string.Empty;
5888
59-
[Required(ErrorMessage = "Please enter your email")]
60-
[EmailAddress(ErrorMessage = "Enter a valid email address")]
61-
public string EmailAddress { get; set; }
89+
[Required]
90+
public string LastName { get; set; } = string.Empty;
6291
}
6392
}
6493
````
6594

66-
## Using Validation Message with EditForm
95+
## Using with EditForm
6796

68-
1. Replace the `<ValidationMessage>` tags with `<TelerikValidationMessage>` tags.
69-
1. Provide a lambda expression in the `For` parameter that sets the associated property of the model, just like with the standard Blazor `ValidationMessage` component.
97+
In an existing Blazor `EditForm`, replace the `<ValidationMessage>` tags with `<TelerikValidationMessage>` tags. The `For` parameter is set in the same way for both validation components.
7098

7199
>caption Use Telerik ValidationMessage in an EditForm
72100
73101
````RAZOR
74102
@using System.ComponentModel.DataAnnotations
75103
76-
<EditForm Model="@customer" width="600px">
104+
<EditForm Model="@Employee" style="width:300px">
77105
<DataAnnotationsValidator />
78106
79-
<InputText @bind-Value="@customer.CustomerName"></InputText>
80-
<TelerikValidationMessage For="@(() => customer.CustomerName)" />
81-
<br />
82-
<InputNumber @bind-Value="@customer.CustomerAge"></InputNumber>
83-
<TelerikValidationMessage For="@(() => customer.CustomerAge)" />
84-
<br />
85-
<InputText @bind-Value="@customer.EmailAddress"></InputText>
86-
<TelerikValidationMessage For="@(() => customer.EmailAddress)" />
87-
<br />
88-
<input type="submit" value="Submit" />
107+
<label for="first-name">First Name</label>
108+
<TelerikTextBox @bind-Value="@Employee.FirstName" Id="first-name" />
109+
<TelerikValidationMessage For="@(() => Employee.FirstName)" />
110+
111+
<label for="last-name">Last Name</label>
112+
<TelerikTextBox @bind-Value="@Employee.LastName" Id="last-name" />
113+
<TelerikValidationMessage For="@(() => Employee.LastName)" />
114+
115+
<div>
116+
<TelerikButton>Submit</TelerikButton>
117+
</div>
89118
</EditForm>
90119
91120
@code {
92-
private Customer customer = new Customer();
121+
private Person Employee { get; set; } = new();
93122
94-
public class Customer
123+
public class Person
95124
{
96-
[Required(ErrorMessage = "Please enter your name")]
97-
[MaxLength(40, ErrorMessage = "The name must be up to 40 characters long")]
98-
public string CustomerName { get; set; }
99-
100-
[Required(ErrorMessage = "Please enter your age")]
101-
[Range(18, 120, ErrorMessage = "You should be at least 18 years old to place an order")]
102-
public int CustomerAge { get; set; }
125+
[Required(ErrorMessage = "Please enter a first name")]
126+
[MinLength(2, ErrorMessage = "The first name must be at least 2 characters long")]
127+
[MaxLength(40, ErrorMessage = "The first name must be up to 40 characters long")]
128+
public string FirstName { get; set; } = string.Empty;
103129
104-
[Required(ErrorMessage = "Please enter your email")]
105-
[EmailAddress(ErrorMessage = "Enter a valid email address")]
106-
public string EmailAddress { get; set; }
130+
[Required]
131+
public string LastName { get; set; } = string.Empty;
107132
}
108133
}
109134
````
110135

111136
## Template
112137

113-
The `TelerikValidationMessage` allows you to control its rendering via a nested `<Template>` tag. The `context` represents an `IEnumerable<string>` collection of all messages for this model property.
138+
The Telerik ValidationMessage allows you to customize its rendering with a nested `<Template>` tag. The template `context` is an `IEnumerable<string>` collection of all messages for the validated model property.
139+
140+
>caption Using ValidationMessage Template
114141
115142
````RAZOR
116143
@using System.ComponentModel.DataAnnotations
117144
118-
<style>
119-
.custom-validation-message {
120-
color: blue;
121-
font-weight: bold;
122-
}
123-
</style>
124-
125-
<TelerikForm Model="@customer" Width="600px"
126-
ValidationMessageType="@FormValidationMessageType.None">
145+
<TelerikForm Model="@Employee"
146+
Width="300px">
127147
<FormValidation>
128148
<DataAnnotationsValidator />
129149
</FormValidation>
130-
131150
<FormItems>
132-
<FormItem Field="@nameof(Customer.CustomerName)" LabelText="Name" />
133-
<TelerikValidationMessage For="@(() => customer.CustomerName)" />
134-
135-
<FormItem Field="@nameof(Customer.CustomerAge)" LabelText="Age" />
136-
<TelerikValidationMessage For="@(() => customer.CustomerAge)">
151+
<FormItem Field="@nameof(Person.FirstName)" LabelText="First Name">
137152
<Template>
138-
@{
139-
IEnumerable<string> validationMessages = context;
140-
141-
@foreach (string message in validationMessages)
142-
{
143-
<div>
144-
<TelerikSvgIcon Icon="@SvgIcon.XOutline"></TelerikSvgIcon>
145-
<span class="custom-validation-message">@message</span>
146-
</div>
147-
}
148-
}
153+
<label for="first-name" class="k-label k-form-label">First Name</label>
154+
<div class="k-form-field-wrap">
155+
<TelerikTextBox @bind-Value="@Employee.FirstName"
156+
Id="first-name" />
157+
<TelerikValidationMessage For="@(() => Employee.FirstName)">
158+
<Template Context="validationMessages">
159+
@foreach (string message in validationMessages)
160+
{
161+
<div>
162+
<span class="k-form-error k-invalid-msg" style="display:flex; gap: .4em;">
163+
<TelerikSvgIcon Icon="@SvgIcon.ExclamationCircle" />
164+
@message
165+
</span>
166+
</div>
167+
}
168+
</Template>
169+
</TelerikValidationMessage>
170+
</div>
149171
</Template>
150-
</TelerikValidationMessage>
151-
152-
<FormItem Field="@nameof(Customer.EmailAddress)" LabelText="Email Address" />
153-
<TelerikValidationMessage For="@(() => customer.EmailAddress)" />
172+
</FormItem>
173+
<FormItem Field="@nameof(Person.LastName)" LabelText="Last Name" />
154174
</FormItems>
155175
</TelerikForm>
156176
157177
@code {
158-
private Customer customer = new Customer();
178+
private Person Employee { get; set; } = new();
159179
160-
public class Customer
180+
public class Person
161181
{
162-
[Required(ErrorMessage = "Please enter your name")]
163-
[MaxLength(40, ErrorMessage = "The name must be up to 40 characters long")]
164-
public string CustomerName { get; set; }
165-
166-
[Required(ErrorMessage = "Please enter your age")]
167-
[Range(18, 120, ErrorMessage = "You should be at least 18 years old to place an order")]
168-
public int CustomerAge { get; set; }
182+
[Required(ErrorMessage = "Please enter a first name")]
183+
[MinLength(2, ErrorMessage = "The first name must be at least 2 characters long")]
184+
[MaxLength(40, ErrorMessage = "The first name must be up to 40 characters long")]
185+
public string FirstName { get; set; } = string.Empty;
169186
170-
[Required(ErrorMessage = "Please enter your email")]
171-
[EmailAddress(ErrorMessage = "Enter a valid email address")]
172-
public string EmailAddress { get; set; }
187+
[Required]
188+
public string LastName { get; set; } = string.Empty;
173189
}
174190
}
175191
````
@@ -178,55 +194,18 @@ The `TelerikValidationMessage` allows you to control its rendering via a nested
178194

179195
Use the `Class` parameter of the Validation Message to add a custom CSS class to the `span.k-form-error`. This element holds the validation message.
180196

181-
>caption Using TelerikValidationMessage Class for EmailAddress.
197+
>caption Using TelerikValidationMessage Class
182198
183-
````RAZOR
184-
@using System.ComponentModel.DataAnnotations
199+
````RAZOR.skip-repl
200+
<TelerikValidationMessage Class="bold-blue"
201+
For="@(() => Employee.FirstName)" />
185202
186203
<style>
187-
.my-email-message {
188-
color: blue;
204+
.bold-blue {
189205
font-weight: bold;
190-
text-decoration: underline;
206+
color: blue;
191207
}
192208
</style>
193-
194-
<TelerikForm Model="@customer" Width="600px"
195-
ValidationMessageType="@FormValidationMessageType.None">
196-
<FormValidation>
197-
<DataAnnotationsValidator />
198-
</FormValidation>
199-
200-
<FormItems>
201-
<FormItem Field="@nameof(Customer.CustomerName)" LabelText="Name" />
202-
<TelerikValidationMessage For="@(() => customer.CustomerName)" />
203-
204-
<FormItem Field="@nameof(Customer.CustomerAge)" LabelText="Age" />
205-
<TelerikValidationMessage For="@(() => customer.CustomerAge)" />
206-
207-
<FormItem Field="@nameof(Customer.EmailAddress)" LabelText="Email Address" />
208-
<TelerikValidationMessage For="@(() => customer.EmailAddress)" Class="my-email-message" />
209-
</FormItems>
210-
</TelerikForm>
211-
212-
@code {
213-
private Customer customer = new Customer();
214-
215-
public class Customer
216-
{
217-
[Required(ErrorMessage = "Please enter your name")]
218-
[MaxLength(40, ErrorMessage = "The name must be up to 40 characters long")]
219-
public string CustomerName { get; set; }
220-
221-
[Required(ErrorMessage = "Please enter your age")]
222-
[Range(18, 120, ErrorMessage = "You should be at least 18 years old to place an order")]
223-
public int CustomerAge { get; set; }
224-
225-
[Required(ErrorMessage = "Please enter your email")]
226-
[EmailAddress(ErrorMessage = "Enter a valid email address")]
227-
public string EmailAddress { get; set; }
228-
}
229-
}
230209
````
231210

232211
## Next Steps
@@ -236,6 +215,6 @@ Use the `Class` parameter of the Validation Message to add a custom CSS class to
236215
## See Also
237216

238217
* [Live Demo: Validation](https://demos.telerik.com/blazor-ui/validation/overview)
239-
* [TelerikValidationSummary](slug:validation-tools-summary)
240-
* [TelerikValidationTooltip](slug:validation-tools-tooltip)
241-
* [Form Component](slug:form-overview)
218+
* [Validate Inputs in Child Components](slug:inputs-kb-validate-child-component)
219+
* [Telerik ValidationSummary](slug:validation-tools-summary)
220+
* [Telerik ValidationTooltip](slug:validation-tools-tooltip)

0 commit comments

Comments
 (0)