# Form Validation

To enable validation in the Form for Blazor you can use the `<FormValidation>` nested tag. The component works with the Microsoft `DataAnnotationsValidator` as well as any validator that is compatible with the `EditForm` and `EditContext` provided by the framework.

In this article:

* [Basics](https://www.telerik.com/blazor-ui/documentation/components/form/validation#basics)
* [Validation Message Type](https://www.telerik.com/blazor-ui/documentation/components/form/validation#validation-message-type)
* [Examples](https://www.telerik.com/blazor-ui/documentation/components/form/validation#examples)
  * [Validate a Model](https://www.telerik.com/blazor-ui/documentation/components/form/validation#validate-a-model)
  * [Validate a Complex Model](https://www.telerik.com/blazor-ui/documentation/components/form/validation#validate-a-complex-model)
  * [Fluent Validation](https://www.telerik.com/blazor-ui/documentation/components/form/validation#fluent-validation)

## Basics

To enable validation in the Telerik Form for Blazor, add the `<FormValidation>` tag inside the `<TelerikForm>`. The `<FormValidation>` provides validation configuration such as a validator (for example, `<DataAnnotationsValidator>`) and validation message UI like the [`<TelerikValidationSummary>`](https://www.telerik.com/blazor-ui/documentation/components/validation/summary).

> caption Enable validation in the Telerik Form for Blazor

<demo metaUrl="client/form/validation/example-1/" height="600"></demo>

> When using the [Form `EditContext` parameter](https://www.telerik.com/blazor-ui/documentation/components/form/overview#creating-blazor-form) together with [validation components](https://www.telerik.com/blazor-ui/documentation/components/validation/overview) or [Form item `<Template>`s](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/template), make sure to create the `EditContext` from the model instance, which is used by the validation components and inside the Form item templates. Otherwise, the Form will not update the correct object instance and validation will not work as expected.

The Telerik Blazor validation tools let you match the style of your validation messages to all other Telerik Blazor components in your app. The validation tools do not expose API or settings for specific validation logic. You need to handle the validation logic separately and then use the Telerik Blazor UI components to display messages to the end user.

When you provide an `EditContext` to the form, you can use its [`EnableDataAnnotationsValidation(IServiceProvider serviceProvider)`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.forms.editcontextdataannotationsextensions.enabledataannotationsvalidation?view=aspnetcore-8.0#microsoft-aspnetcore-components-forms-editcontextdataannotationsextensions-enabledataannotationsvalidation\(microsoft-aspnetcore-components-forms-editcontext-system-iserviceprovider\)) method to add the data annotation validation instead of using the markup. This is useful in the following cases:

* When the Form `Model` changes at runtime. For example, when you [have a reset button](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/buttons#how-to-add-a-reset-clear-button-to-the-form).
* When you need to re-attach the validation after changing the `Model`. In this case you need to use the [Form component reference](https://www.telerik.com/blazor-ui/documentation/components/form/overview#form-reference-and-methods)—`FormRef.EditContext.EnableDataAnnotationsValidation(IServiceProvider serviceProvider)`.

Telerik Blazor input components automatically display an invalid state when their value does not match the Form validation rules. This behavior depends on the input's `ValueExpression`. See how to [set the `ValueExpression` correctly when the input is nested in a child component of the Form](https://www.telerik.com/blazor-ui/documentation/knowledge-base/inputs-validation-child-component).

> The Telerik components for Blazor do not perform the actual validation of the model. Validation is managed by the [`EditContext`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.forms.editcontext). The role of the Telerik components is to call `EditContext` methods, subscribe to `EditContext` events, retrieve validation messages, and display them. If a validation scenario does not work as expected, check the behavior in a standard Blazor `<EditForm>` to verify if the issue is related to the Telerik components.

## Validation Message Type

With the `ValidationMessageType` parameter of the Telerik Form for Blazor you can customize the way the validation messages are presented to the user. This setting accepts a member of the `FormValidationMessageType` enum:

* `Tooltip` - validation messages will show up in a tooltip pointing to the invalid input.
* `Inline` - the standard display of messages in text after the input. This is the default value.
* `None` - no validation messages will be rendered. The user will be notified that certain field is incorrect by a red border around the associated editor.

> caption Display validation messages in tooltips

```RAZOR.skip-repl
<TelerikForm ValidationMessageType="@FormValidationMessageType.Tooltip" />
```

## Examples

This section discusses the following validation scenarios:

* [Fluent Validation](https://www.telerik.com/blazor-ui/documentation/components/form/validation#fluent-validation)
* [Validate Nested Properties or Collections](https://www.telerik.com/blazor-ui/documentation/components/form/validation#validate-a-nested-property-or-collection)

Also check the following pages for information on how to:

* [Trigger Form validation programmatically](https://www.telerik.com/blazor-ui/documentation/components/form/overview#form-reference-and-methods)
* [Use custom DataAnnotations validation](https://www.telerik.com/blazor-ui/documentation/knowledge-base/validation-custom-dataannotations-validator)
* [Apply invalid state to an editor that is inside a child component](https://www.telerik.com/blazor-ui/documentation/knowledge-base/inputs-validation-child-component)

### Fluent Validation

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.

The example below:

* Requires the [`Blazilla` NuGet package](https://www.nuget.org/packages/Blazilla). Also refer to the [FluentValidation documentation](https://docs.fluentvalidation.net/en/latest/blazor.html).
* Shows how to pass `ValueExpression` from a parent component to optional custom child components in a [Form item template](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/template) or a [Grid editor template](https://www.telerik.com/blazor-ui/documentation/components/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'`](https://www.telerik.com/blazor-ui/documentation/knowledge-base/form-fluent-validation-cannot-validate-instances-of-type).

> Telerik does not endorse any particular third-party validator. From Telerik API perspective, the integration is the same with any validator. To run the example with the popular (but obsolete) `Blazored.FluentValidation` library instead of `Blazilla`:
>
> * Install the [`Blazored.FluentValidation`](https://www.nuget.org/packages/Blazored.FluentValidation) NuGet package
> * Use `@using Blazored.FluentValidation` instead of `@using Blazilla`
> * Use `<FluentValidationValidator>` instead of `<FluentValidator>`

> caption Using FluentValidation

```RAZOR Home.razor
@using Blazilla
@using FluentValidation

<TelerikForm Model="@PersonToEdit"
             OnValidSubmit="@OnFormValidSubmit">
    <FormValidation>
        <FluentValidator Validator="@PersonFluentValidator" />
        <TelerikValidationSummary />
    </FormValidation>
    <FormItems>
        <FormItem Field="@nameof(Person.Id)" Enabled="false" LabelText="ID" />
        <FormItem Field="@nameof(Person.FirstName)" LabelText="First Name" />
        <FormItem Field="@nameof(Person.MiddleName)">
            <Template>
                <label for="person-middlename" class="k-label k-form-label">Middle Name (two-way binding)</label>
                <div class="k-form-field-wrap">
                    <TextBox @bind-Value="@PersonToEdit.MiddleName"
                             Id="person-middlename" />
                    <TelerikValidationMessage For="@( () => PersonToEdit.MiddleName )" />
                </div>
            </Template>
        </FormItem>
        <FormItem Field="@nameof(Person.LastName)">
            <Template>
                <label for="person-lastname" class="k-label k-form-label">Last Name (one-way binding with explicit ValueExpression)</label>
                <div class="k-form-field-wrap">
                    <TextBox Value="@PersonToEdit.LastName"
                             ValueChanged="@LastNameChanged"
                             ValueExpression="@( () => PersonToEdit.LastName )"
                             Id="person-lastname" />
                    <TelerikValidationMessage For="@( () => PersonToEdit.LastName )" />
                </div>
            </Template>
        </FormItem>
    </FormItems>
</TelerikForm>

<p style="color:var(--kendo-color-success)"><strong>@FormSubmitResult</strong></p>

@code {
    private Person PersonToEdit { get; set; } = new();

    public PersonValidator PersonFluentValidator { get; set; } = new();

    private string FormSubmitResult { get; set; } = string.Empty;

    private void LastNameChanged(string newLastName)
    {
        PersonToEdit.LastName = newLastName;
    }

    private void OnFormValidSubmit()
    {
        FormSubmitResult = $"Form Submit Success at {DateTime.Now.ToString("HH:mm:ss")}";
    }

    public class PersonValidator : AbstractValidator<Person>
    {
        public PersonValidator()
        {
            RuleFor(customer => customer.FirstName).NotEmpty().MinimumLength(2).MaximumLength(60);
            RuleFor(customer => customer.MiddleName).NotEmpty().MaximumLength(60);
            RuleFor(customer => customer.LastName).NotEmpty().MinimumLength(2).MaximumLength(60);
        }
    }

    public class Person
    {
        public int Id { get; set; }

        public string FirstName { get; set; } = string.Empty;

        public string MiddleName { get; set; } = string.Empty;

        public string LastName { get; set; } = string.Empty;
    }
}
```

```RAZOR TextBox.razor
@using System.Linq.Expressions

<TelerikTextBox Value="@Value"
                ValueChanged="@ValueChanged"
                ValueExpression="@ValueExpression"
                Id="@Id" />

@code {
    [Parameter]
    public string Value { get; set; } = string.Empty;

    [Parameter]
    public EventCallback<string> ValueChanged { get; set; }

    [Parameter]
    public Expression<System.Func<string>>? ValueExpression { get; set; }

    [Parameter]
    public string Id { get; set; } = string.Empty;

    private async Task TextBoxValueChanged(string newValue)
    {
        Value = newValue;

        if (ValueChanged.HasDelegate)
        {
            await ValueChanged.InvokeAsync(newValue);
        }
    }
}
```

### Validate Nested Properties or Collections

Refer to the [Microsoft documentation about validating nested objects and collection types](https://learn.microsoft.com/en-us/aspnet/core/blazor/forms/validation).

When using a model with nested objects and properties, [specify their `Field` settings as dot-separate strings](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-bind-navigation-property-complex-object). Do not use a single `nameof()` operator, because it does not return the full name of the nested property.

The example below demonstrates how to validate a nested property `Product.Category.Id` and a collection `Product.Ingredients`. The sample code assumes that:

* The project name and root namespace is `TelerikBlazorApp`.
* The model classes belong to the `TelerikBlazorApp.Data` namespace.

<div class="skip-repl">

```RAZOR Home.razor
<TelerikForm Model="@ProductModel"
             Orientation="@FormOrientation.Horizontal"
             Width="480px">
    <FormValidation>
        <DataAnnotationsValidator />
        <TelerikValidationSummary />
    </FormValidation>
    <FormItems>
        <FormItem Field="@nameof(Product.Name)" LabelText="Product Name"></FormItem>
        <FormItem Field="@($"{nameof(Product.Category)}.{nameof(Category.Id)}")" LabelText="Category">
            <Template>
                <label for="category" class="k-label k-form-label">Category</label>
                <div class="k-form-field-wrap">
                    <TelerikDropDownList @bind-Value="@ProductModel.Category.Id"
                                         DefaultText="Select Category"
                                         Data="@Categories"
                                         TextField="@nameof(Category.Name)"
                                         ValueField="@nameof(Category.Id)"
                                         Id="category">
                        <DropDownListSettings>
                            <DropDownListPopupSettings Height="auto" />
                        </DropDownListSettings>
                    </TelerikDropDownList>
                    <TelerikValidationMessage For="@(() => ProductModel.Category.Id)" />
                </div>
            </Template>
        </FormItem>
        <FormItem Field="@nameof(Product.ReleaseDate)" LabelText="Release Date"></FormItem>
        <FormItem Field="@nameof(Product.Ingredients)">
            <Template>
                <label class="k-label k-form-label ingredients-label">Ingredients</label>
                <div class="k-form-field-wrap ingredients-wrap">
                    <div class="ingredients">
                        <TelerikValidationMessage For="@(() => ProductModel.Ingredients)" />
                        @foreach (Ingredient ingredient in ProductModel.Ingredients ?? new List<Ingredient>())
                        {
                            <div>
                                <div class="ingredient">
                                    <label>Name</label>
                                    <TelerikTextBox @bind-Value="@ingredient.Name" />
                                    <TelerikButton Icon="@SvgIcon.Trash"
                                                OnClick="@(() => OnRemoveIngredient(ingredient))" />
                                </div>
                                <div>
                                    <TelerikValidationMessage For="@(() => ingredient.Name)" />
                                </div>
                            </div>
                        }
                        <div class="add-ingredient">
                            <TelerikButton Icon="@SvgIcon.Plus"
                                           OnClick="@OnAddIngredient">
                                Add Ingredient
                            </TelerikButton>
                        </div>
                    </div>
                </div>
            </Template>
        </FormItem>
    </FormItems>
</TelerikForm>

<style>
    .ingredients-label {
        margin-top: var(--kendo-spacing-3\.5);
    }

    .ingredients-wrap {
        border: 1px solid var(--kendo-color-border);
        padding: 1em;
        border-radius: var(--kendo-border-radius-md);
    }

    .ingredients {
        display: flex;
        flex-direction: column;
        gap: var(--kendo-spacing-4);
    }

    .ingredient {
        display: flex;
        align-items: center;
        gap: 1em;
        margin-bottom: var(--kendo-spacing-4);
    }

    .ingredient > label {
        flex: 1 1 40%;
    }

    .ingredient + .add-ingredient {
        margin-top: var(--kendo-spacing-4);
    }

    div.ingredient:has(.k-invalid) label {
        color: var(--kendo-color-error-on-surface);
    }
</style>

@code {
    private Product ProductModel { get; set; } = new();
    private readonly Category[] Categories = new Category[]
    {
        new Category { Id = 1, Name = "First Category" },
        new Category { Id = 2, Name = "Second Category" },
        new Category { Id = 3, Name = "Third Category" }
    };

    private void OnAddIngredient()
    {
        ProductModel.Ingredients ??= new List<Ingredient>();
        ProductModel.Ingredients.Add(new Ingredient());
    }

    private void OnRemoveIngredient(Ingredient ingredient)
    {
        ProductModel.Ingredients?.Remove(ingredient);

        if (ProductModel.Ingredients?.Count == 0)
        {
            ProductModel.Ingredients = default;
        }
    }
}
```

```C# Program.cs
builder.Services.AddValidation();
```

```C# Category.cs
using System.ComponentModel.DataAnnotations;

namespace TelerikBlazorApp.Data;

public class Category
{
    [Required(ErrorMessage = "Category is required.")]
    public int? Id { get; set; }

    public string Name { get; set; } = string.Empty;
}
```

```C# Product.cs
using System.ComponentModel.DataAnnotations;

namespace TelerikBlazorApp.Data;

[ValidatableType]
public class Product
{
    public string Id { get; set; } = Guid.NewGuid().ToString();

    [Required(ErrorMessage = "Product Name is required.")]
    public string Name { get; set; } = string.Empty;

    public Category Category { get; set; } = new Category();

    [Required(ErrorMessage = "Release Date is required.")]
    public DateTime? ReleaseDate { get; set; }

    [Required(ErrorMessage = "At least one ingredient is required.")]
    public List<Ingredient>? Ingredients { get; set; }
}
```

```C# Ingredient.cs
using System.ComponentModel.DataAnnotations;

namespace TelerikBlazorApp.Data;

public class Ingredient
{
    public string Id { get; set; } = Guid.NewGuid().ToString();

    [Required(ErrorMessage = "Ingredient Name is required.")]
    public string Name { get; set; } = string.Empty;
}
```

## See Also

* [Custom Form `DataAnnotations` Validation](https://www.telerik.com/blazor-ui/documentation/knowledge-base/validation-custom-dataannotations-validator)
* [Conditional Form Validation Options](https://www.telerik.com/blazor-ui/documentation/knowledge-base/form-conditional-validation)
* [Display Invalid State in Child Component](https://www.telerik.com/blazor-ui/documentation/knowledge-base/inputs-validation-child-component)
