Close Menu
Soshace Digital Blog

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    What's Hot
    Recruitment

    Enhancing Recruitment: The Crucial Role of Diversity and Inclusion

    JavaScript

    Maximizing Efficiency: Utilizing Project Dashboards for Progress Tracking

    Startups

    Effective Low-Cost Marketing Strategies for Startups

    Important Pages:
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Today's Picks:
    • Scaling Success: Monitoring Indexation of Programmatic SEO Content
    • Leveraging Influencers: Key Drivers in New Product Launches
    • How Privacy-First Marketing Will Transform the Industry Landscape
    • The Impact of Social Proof on Thought Leadership Marketing
    • Balancing Value-Driven Content and Promotional Messaging Strategies
    • Top Influencer Marketing Platforms to Explore in 2025
    • Emerging Trends in Marketing Automation and AI Tools for 2023
    • Strategies to Mitigate Duplicate Content in Programmatic SEO
    Tuesday, September 9
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Soshace Digital Blog
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Services
    • SaaS & Tech

      Maximizing Efficiency: How SaaS Lowers IT Infrastructure Costs

      August 27, 2025

      Navigating Tomorrow: Innovations Shaping the Future of SaaS

      August 27, 2025

      Maximizing Impact: Strategies for SaaS & Technology Marketing

      August 27, 2025
    • AI & Automation

      Enhancing Customer Feedback Analysis Through AI Innovations

      August 27, 2025

      Navigating the Impact of AI on SEO and Search Rankings

      August 27, 2025

      5 Automation Hacks Every Home Service Business Needs to Know

      May 3, 2025
    • Finance & Fintech

      Critical Missteps in Finance Marketing: What to Avoid

      August 27, 2025

      Analyzing Future Fintech Marketing Trends: Insights Ahead

      August 27, 2025

      Navigating the Complex Landscape of Finance and Fintech Marketing

      August 27, 2025
    • Legal & Compliance

      Exploring Thought Leadership’s Impact on Legal Marketing

      August 27, 2025

      Maximizing LinkedIn: Strategies for Legal and Compliance Marketing

      August 27, 2025

      Why Transparency Matters in Legal Advertising Practices

      August 27, 2025
    • Medical Marketing

      Enhancing Online Reputation Management in Hospitals: A Guide

      August 27, 2025

      Analyzing Emerging Trends in Health and Medical Marketing

      August 27, 2025

      Exploring Innovative Content Ideas for Wellness Blogs and Clinics

      August 27, 2025
    • E-commerce & Retail

      Strategic Seasonal Campaign Concepts for Online and Retail Markets

      August 27, 2025

      Emerging Trends in E-commerce and Retail Marketing Strategies

      August 27, 2025

      Maximizing Revenue: The Advantages of Affiliate Marketing for E-Commerce

      August 27, 2025
    • Influencer & Community

      Leveraging Influencers: Key Drivers in New Product Launches

      August 27, 2025

      Top Influencer Marketing Platforms to Explore in 2025

      August 27, 2025

      Key Strategies for Successful Influencer Partnership Negotiations

      August 27, 2025
    • Content & Leadership

      The Impact of Social Proof on Thought Leadership Marketing

      August 27, 2025

      Balancing Value-Driven Content and Promotional Messaging Strategies

      August 27, 2025

      Analyzing Storytelling’s Impact on Content Marketing Effectiveness

      August 27, 2025
    • SEO & Analytics

      Scaling Success: Monitoring Indexation of Programmatic SEO Content

      August 27, 2025

      Strategies to Mitigate Duplicate Content in Programmatic SEO

      August 27, 2025

      Effective Data Visualization Techniques for SEO Reporting

      August 27, 2025
    • Marketing Trends

      How Privacy-First Marketing Will Transform the Industry Landscape

      August 27, 2025

      Emerging Trends in Marketing Automation and AI Tools for 2023

      August 27, 2025

      Maximizing ROI: Key Trends in Paid Social Advertising

      August 27, 2025
    Soshace Digital Blog
    Blog / ASP.NET / Fluent Validation in ASP.NET MVC
    ASP.NET

    Fluent Validation in ASP.NET MVC

    KapilBy KapilDecember 28, 2020Updated:May 26, 2024No Comments7 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr Email Reddit
    Fluent Validation in ASP.NET MVC
    Fluent Validation in ASP.NET MVC
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link
    Fluent Validation in ASP.NET MVC
    Fluent Validation in ASP.NET MVC

    Introduction

    Validation is important for any website. Validation makes sure that user input is valid or not. If the input is not valid then throw a validation message. In fact, if you observe the latest Asp.Net technologies, you could find out that the validation feature was given extreme importance with fine updates.

    Asp.net MVC provides a set of validations that is easy to use. It is the best way to deal with errors. We all knew about the validation through data annotation. Now we will see with the help of fluent validation.

    What is Fluent Validation?

    Fluent validation is a validation library for .NET. This will be separated from the business logic. So, it has an advantage over data annotation validation. This library is available in the NuGet package so it is easy to add to the application.

    Example: 

    Let’s take a simple example.

    Step 1: – Add Fluent Validation Package

    To use the fluent validators, we first need to download it from the NuGet packages. Just write the “FluentValidation” and download version 8.6.1 since “FluentValidation.Mvc5” is deprecated.

    Fluent Validation Package
    Fluent Validation Package

    Step 2: – Create a view model

    Now let’s take a view model class to apply fluent validation. We have named it Client View Model, you can give it to yours. There are three properties in the view model class as described below in the snippet.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace FluentValidatorMVC.Model
    {
        public class ClientViewModel
        {
            public string ValidateUserName { get; set; }
            public string ValidateEmailAddress { get; set; }
            public string ValidatePassword { get; set; }
        }
    }
    

    There are three properties in ClientViewModel, “ValidateUserName”,” ValidateEmailAddress”, and” ValidatePassword”.

    Step 3: – Create the validator class

    Now we will create the validator class for our view model class to perform the validation on it. Inherits this class to the “AbstractValidator”. It is the base class for object validators.

    using FluentValidation;
    using FluentValidatorMVC.Model;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace FluentValidatorMVC.Models
    {
        public class ValidatorModel : AbstractValidator<ClientViewModel>
        {
            public ValidatorModel()
            {
                RuleFor(x => x.ValidateUserName).NotEmpty().WithMessage("Required Field");
                RuleFor(x => x.ValidateEmailAddress).EmailAddress().WithMessage("Please enter valid email address").NotEmpty().WithMessage("Required 
    Field");
                RuleFor(x => x.ValidatePassword).NotEmpty().WithMessage("Required Field").Length(4,8);
            }
        }
    }
    

    Here RuleFor defines the specific rule for the specific property. We have passed the view model in the base class validator. We have used the validation for not empty text fields, for an email address, and for password length.

    If any of these fields is null or empty then it will show a validation message using the “WithMessage” property.

    Read More:  Why Using Node.js for Developing Web Applications Makes Perfect Sense

    Apart from this, there are other validation properties like Must (for age validation), Equal (for password field), and InclusiveBetween (for range validation).

    Step 4: – Create the Factory class

    We will create the Fluent validator factory which inherits from the validator factory base class. Validator factory base class is the factory for creating validators.

    using FluentValidation;
    using FluentValidatorMVC.Model;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace FluentValidatorMVC.Models
    {
        public class FluentValidatorFactory : ValidatorFactoryBase
        {
            private static Dictionary<Type, IValidator> validators = new Dictionary<Type, IValidator>();
    
            static FluentValidatorFactory()
            {
                validators.Add(typeof(IValidator<ClientViewModel>), new ValidatorModel());
            }
    
            public override IValidator CreateInstance(Type validatorType)
            {
                IValidator validator;
                if(validators.TryGetValue(validatorType, out validator))
                {
                    return validator;
                }
                return validator;
            }
        } 
    }
    

    As we can see here that, we have to inherit the factory base class and created two static methods. We have taken here the static dictionary variable and static constructor. The Dictionary variable represents the key-value pair. Here the key is the type and the value is IValidator. IValidator gets the validation rule from the validator class at run time on the view model class.

    We have defined the static constructor because the validation rule will be applied or registered on the related view model one single time when the application run.

    Step 5: – Execute validation factory

    Now we need to execute the validation factory constructor when the application runs, so create the instance of it in the global. asax file as shown below.

    using FluentValidation.Mvc;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Optimization;
    using System.Web.Routing;
    using FluentValidatorMVC.Models;
    
    namespace FluentValidatorMVC
    {
        public class MvcApplication : System.Web.HttpApplication
        {
            protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
                ValidatorConfiguration();
            }
    
            private void ValidatorConfiguration()
            {
                FluentValidationModelValidatorProvider.Configure(provider =>
                {
                    provider.ValidatorFactory = new FluentValidatorFactory();
                });
            }
        }
    }
    

    As we can see here, we have created the validation configuration method and, in the method, we have defined the validator factory class.

    Step 6: – Create the controller

    First, create the controller and two action methods for view and insert the value in it.

    using FluentValidatorMVC.Model;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace FluentValidatorMVC.Controllers
    {
        public class ValidatorController : Controller
        {
            // GET: Validator
            public ActionResult Index()
            {
                return View();
            }
            [HttpGet]
            public ActionResult AddClient()
            {
                return View();
            }
            [HttpPost]
            public ActionResult AddClient(ClientViewModel clientmodel)
            {
                if (ModelState.IsValid)
                {
                    //your code 
                }
                return View();
            }
        }
    }
    

    Step 7: – Create the view

    Now create the view of this method and run the project. In the view, place below code.

    @model FluentValidatorMVC.Model.ClientViewModel
    @{
        ViewBag.Title = "AddClient";
    }
    
    <h2>AddClient</h2>
    
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
    
        <div class="form-horizontal">
            <h4>Add User</h4>
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    
            <div class="form-group">
                @Html.LabelFor(model => model.ValidateUserName, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.ValidateUserName, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.ValidateUserName, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.ValidateEmailAddress, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.ValidateEmailAddress, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.ValidateEmailAddress, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.ValidatePassword, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.ValidatePassword, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.ValidatePassword, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Create" class="btn btn-default" />
                </div>
            </div>
        </div>
    }
    
    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>
    
    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
    }
    

    Output:

    Read More:  Node.js Lesson 9: Events, EventEmitter and Memory Leaks
    Required Field Validation
    Required Field Validation
    Email, and Password Validation
    Email, and Password Validation

    We can do this validation using the validator attribute as shown below.

    using FluentValidation.Attributes;
    using FluentValidatorMVC.Models;
    
    namespace FluentValidatorMVC.Model
    {
        [Validator(typeof(ValidatorModel))]
        public class ClientViewModel
        {
            public string ValidateUserName { get; set; }
            public string ValidateEmailAddress { get; set; }
            public string ValidatePassword { get; set; }
        }
    }
    

    So, using this validator attribute we can perform the validation. And do change in global. asax file. Just add one configure method in the application_start method.

    using FluentValidation.Mvc;
    using FluentValidatorMVC.Models;
    
    namespace FluentValidatorMVC
    {
        public class MvcApplication : System.Web.HttpApplication
        {
            protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
                FluentValidationModelValidatorProvider.Configure();
            }
        }
    }
    

    Client-Side Validation: –

    Note here that fluent validation also supports the client-side validation in MVC but there are only some rules that are supported. Which we have listed below.

    • Not Null/Not Empty
    • Regex validation
    • Range validation
    • Email
    • Equal to
    • Length

    Conclusion

    In this blog, we have seen how to download the fluent package and how to use this fluent validator in MVC. This is simple and easy to add new validation because it will reside in the specific validator class file. But in MVC, we can add the data annotation above the class or property to validate it.

    Author Bio: Vinod Satapara – Technical Director, iFour Technolab Netherlands.

    Technocrat and entrepreneur with years of experience building large-scale enterprise web, cloud, and mobile applications using the latest technologies like ASP.NET, CORE, .NET MVC, Angular, and Blockchain. Keen interest in addressing business problems using the latest technologies and have been associated with ASP.NET software development companies.

    Author Vinod Satapara
    Author Vinod Satapara

    LinkedIn: https://www.linkedin.com/in/vinodsatapara/

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Kapil

      Related Posts

      Mastering REST APIs: Essential Techniques for Programmers

      December 18, 2024

      Crafting Interactive User Interfaces Using JavaScript Techniques

      December 17, 2024

      Effective Strategies for Utilizing Frameworks in Web Development

      December 16, 2024
      Leave A Reply Cancel Reply

      You must be logged in to post a comment.

      Stay In Touch
      • Facebook
      • Twitter
      • Pinterest
      • Instagram
      • YouTube
      • Vimeo
      Don't Miss
      Interview April 25, 2019

      Interview with Anuar Akhmetov: I Was Apathetic of Programming until I Met My Very Best Mentor

      I talked to Anuar Akhmetov, a Python developer employed by Soshace, who related his passion for programming, talked about finding the right mentor, changing the education system in post-soviet countries, and finally, how freelance could possibly change anyone’s life for the better.

      Build Real-world React Native App #1: Splash screen and App Icon

      November 9, 2020

      React Lesson 5: React Devtools and Reusable Open-Source Components

      December 20, 2019

      An Introduction to Pinia: The Alternative State Management Library for Vue.js Applications

      March 29, 2023

      Categories

      • AI & Automation
      • Angular
      • ASP.NET
      • AWS
      • B2B Leads
      • Beginners
      • Blogs
      • Business Growth
      • Case Studies
      • Comics
      • Consultation
      • Content & Leadership
      • CSS
      • Development
      • Django
      • E-commerce & Retail
      • Entrepreneurs
      • Entrepreneurship
      • Events
      • Express.js
      • Facebook Ads
      • Finance & Fintech
      • Flask
      • Flutter
      • Franchising
      • Funnel Strategy
      • Git
      • GraphQL
      • Home Services Marketing
      • Influencer & Community
      • Interview
      • Java
      • Java Spring
      • JavaScript
      • Job
      • Laravel
      • Lead Generation
      • Legal & Compliance
      • LinkedIn
      • Machine Learning
      • Marketing Trends
      • Medical Marketing
      • MSP Lead Generation
      • MSP Marketing
      • NestJS
      • Next.js
      • Node.js
      • Node.js Lessons
      • Paid Advertising
      • PHP
      • Podcasts
      • POS Tutorial
      • Programming
      • Programming
      • Python
      • React
      • React Lessons
      • React Native
      • React Native Lessons
      • Recruitment
      • Remote Job
      • SaaS & Tech
      • SEO & Analytics
      • Soshace
      • Startups
      • Swarm Intelligence
      • Tips
      • Trends
      • Vue
      • Wiki
      • WordPress
      Top Posts

      Top IT Conferences and Events in 2020

      Events January 3, 2020

      Strategic Talent Pool Development for Future Hiring Success

      Recruitment December 9, 2024

      Tips and Tricks for a Newbie at Web Development: How to Prepare for the Interview

      Interview December 10, 2019

      Инструменты JavaScript / Node.js разработчика

      Programming January 14, 2016

      Subscribe to Updates

      Get The Latest News, Updates, And Amazing Offers

      About Us
      About Us

      Soshace Digital delivers comprehensive web design and development solutions tailored to your business objectives. Your website will be meticulously designed and developed by our team of seasoned professionals, who combine creative expertise with technical excellence to transform your vision into a high-impact, user-centric digital experience that elevates your brand and drives measurable results.

      7901 4th St N, Suite 28690
      Saint Petersburg, FL 33702-4305
      Phone: 1(877)SOSHACE

      Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
      Our Picks
      Interview

      Interview with Alex

      Blogs

      16 Best Web Development & Web Design Blogs

      Node.js

      Nodejs Lesson 16: Internals of Nodejs: Event Loop

      Most Popular

      Analyzing LinkedIn Recommendations: A Key to Lead Generation

      LinkedIn

      Interview with Philipp

      Interview

      Java Stream API

      Beginners
      © 2025 Soshace Digital.
      • Home
      • About
      • Services
      • Contact Us
      • Privacy Policy
      • Terms & Conditions

      Type above and press Enter to search. Press Esc to cancel.