Blazor Form Validation
Validation with DataAnnotations public class Employee { [Required(ErrorMessage = "Name is required")] public string Name { get; set; } } @page "/employee" @using System.ComponentModel.DataAnnotations @using Microsoft.AspNetCore.Components.Forms <h3>Employee Form</h3> <EditForm Model="@employee" OnValidSubmit="@HandleValidSubmit"> <DataAnnotationsValidator /> <ValidationSummary /> <div class="form-group"> <label for="Name">Name</label> <InputText id="Name" @bind-Value="employee.Name" class="form-control" /> <ValidationMessage For="@(() => employee.Name)" /> </div> <button type="submit" class="btn btn-primary">Submit</button> </EditForm> @code { private Employee employee = new Employee(); } Validation with FluentValidation dotnet add package Blazored.FluentValidation public class EmployeeValidator : AbstractValidator<Employee> { public EmployeeValidator() { RuleFor(x => x.Name).NotEmpty().WithMessage("Name is required"); } } @page "/employee" @using FluentValidation @using FluentValidation.Results <h3>Employee Form</h3> <EditForm Model="@employee" OnValidSubmit="@HandleValidSubmit"> <FluentValidationValidator DisableAssemblyScanning="@true" /> <ValidationSummary /> <div class="form-group"> <label for="Name">Name</label> <InputText id="Name" @bind-Value="employee.Name" class="form-control" /> <ValidationMessage For="@(() => employee.Name)" /> </div> <button type="submit" class="btn btn-primary">Submit</button> </EditForm> Custom Validation