Often the fluent validation is a validation class library for the .NET framework and it uses lambda expressions for building validation rules for your business objects. If you want to use general validation in asp.net MVC application then data annotations validation is good but in case if you want to implement complex validation then you need to use Fluent Validation.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FluentValidation;
namespace MyFirstAppMVC.Models
{
public class EmpValidator : AbstractValidator<Emp>
{
public EmpValidator()
{
RuleFor(x => x.EmpName).NotEmpty().WithMessage("EmpName is required");
RuleFor(x => x.EmpDateOfBirth).NotEmpty().WithMessage("EmpDateOfBirth is required");
RuleFor(x => x.EmpDateOfBirth).Must(AgeValidate).WithMessage("Invalid date Emp age must be 18 or greater than 18");
RuleFor(x => x.EmpEmailID).NotEmpty().WithMessage("Emp EmailID is required");
RuleFor(x => x.EmpEmailID).EmailAddress().WithMessage("Emp EmailID is required");
RuleFor(x => x.EmpAddress).NotEmpty().WithMessage("EmpAddress is required");
RuleFor(x => x.EmpAddress).Length(20, 250).WithMessage("Address must be between 20 to 250 words");
RuleFor(x => x.Password).NotEmpty().WithMessage("Password is required");
RuleFor(x => x.ConfirmPassword).NotEmpty().WithMessage("ConfirmPassword is required");
RuleFor(x => x.ConfirmPassword).Equal(x => x.Password).WithMessage("Password do not Match");
}
private bool AgeValidate(DateTime value)
{
DateTime now = DateTime.Today;
int age = now.Year - Convert.ToDateTime(value).Year;
if (age < 18)
{
return false;
}
else
{
return true;
}
}
}
}
public EmpValidator()
{
RuleFor(x => x.EmpName).NotEmpty().WithMessage("Student Name is required");
RuleFor(x => x.EmpDateOfBirth).NotEmpty().WithMessage("EmpDateOfBirth is required");
RuleFor(x => x.EmpEmailID).NotEmpty().WithMessage("Emp EmailID is required");
RuleFor(x => x.EmpAddress).NotEmpty().WithMessage("Emp Address is required");
RuleFor(x => x.Password).NotEmpty().WithMessage("Password is required");
RuleFor(x => x.ConfirmPassword).NotEmpty().WithMessage("Confirm Password is required");
}
Age Validation Method
private bool AgeValidate(DateTime value) { DateTime now = DateTime.Today; int age = now.Year - Convert.ToDateTime(value).Year; if (age < 18) { return false; } else { return true; } }
A rule for Date of Birth
RuleFor(x => x.StudentDOB).Must(AgeValidate).WithMessage("Invalid date student age must be 18 or greater than 18");
Validating EmailID (Email Address)
RuleFor(x => x.StudentEmailID).EmailAddress().WithMessage("Student EmailID is required");
Etc...
All the examples above are related to "Fluent validation ".
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Thanks, Sanat Sir
Your answer is very helpful.
"Fluent validation in MVC"...
Often the fluent validation is a validation class library for the .NET framework and it uses lambda expressions for building validation rules for your business objects. If you want to use general validation in asp.net MVC application then data annotations validation is good but in case if you want to implement complex validation then you need to use Fluent Validation.
Age Validation Method
A rule for Date of Birth
RuleFor(x => x.StudentDOB).Must(AgeValidate).WithMessage("Invalid date student age must be 18 or greater than 18");Validating EmailID (Email Address)
RuleFor(x => x.StudentEmailID).EmailAddress().WithMessage("Student EmailID is required");Etc...
All the examples above are related to "Fluent validation ".