What is Fluent Validation in MVC ?

Total Post:44

Points:310
C#
 502  View(s)
Ratings:
Rate this:

Fluent Validation in MVC

  1. Post:87

    Points:615
    Re: What is Fluent Validation in MVC ?

    "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.

    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 ".

  1. Post:8

    Points:56
    Re: What is Fluent Validation in MVC ?

    Thanks, Sanat Sir

    Your answer is very helpful.

Answer

Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.