
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
What is use of fluent Validation in C# and how to use in C#?
FluentValidation is a .NET library for building strongly-typed validation rules. It Uses a fluent interface and lambda expressions for building validation rules. It helps clean up your domain code and make it more cohesive, as well as giving you a single place to look for validation logic
To make use of fluent validation we have to install the below package
<PackageReference Include="FluentValidation" Version="9.2.2" />
Example 1
static class Program { static void Main (string[] args) { List errors = new List(); PersonModel person = new PersonModel(); person.FirstName = ""; person.LastName = "S"; person.AccountBalance = 100; person.DateOfBirth = DateTime.Now.Date; PersonValidator validator = new PersonValidator(); ValidationResult results = validator.Validate(person); if (results.IsValid == false) { foreach (ValidationFailure failure in results.Errors) { errors.Add(failure.ErrorMessage); } } foreach (var item in errors) { Console.WriteLine(item); } Console.ReadLine (); } } public class PersonModel { public string FirstName { get; set; } public string LastName { get; set; } public decimal AccountBalance { get; set; } public DateTime DateOfBirth { get; set; } } public class PersonValidator : AbstractValidator { public PersonValidator(){ RuleFor(p => p.FirstName) .Cascade(CascadeMode.StopOnFirstFailure) .NotEmpty().WithMessage("{PropertyName} is Empty") .Length(2, 50).WithMessage("Length ({TotalLength}) of {PropertyName} Invalid") .Must(BeAValidName).WithMessage("{PropertyName} Contains Invalid Characters"); RuleFor(p => p.LastName) .Cascade(CascadeMode.StopOnFirstFailure) .NotEmpty().WithMessage("{PropertyName} is Empty") .Length(2, 50).WithMessage("Length ({TotalLength}) of {PropertyName} Invalid") .Must(BeAValidName).WithMessage("{PropertyName} Contains Invalid Characters"); } protected bool BeAValidName(string name) { name = name.Replace(" ", ""); name = name.Replace("-", ""); return name.All(Char.IsLetter); } }
Output
First Name is Empty Length (1) of Last Name Invalid
- Related Articles
- How to check Minlength and Maxlength validation of a property in C# using Fluent Validation?
- Excel data validation: Add, use, copy and remove data validation in Excel
- How to valid DateofBirth using fluent Validation in C# if it exceeds current year?
- What is SignalR and how to use it?
- What is method hiding in Java and how to use it?
- What is #if DEBUG and How to use it in C#?
- What is the ‘if’ statement in Java and how to use it?
- What is a break statement in Java and how to use it?
- What is a continue statement in Java and how to use it?
- What is the use of $Lastexitcode and $? Variable in PowerShell?
- What is the use and syntax of SEQUENCE in DB2?
- What is electricity and how we use it?
- What is the type conversion operator ( ) in Java and how to use it?
- What is the ‘if else’ statement in Java and how to use it?
- What is the ‘nested if’ statement in Java and how to use it?

Advertisements