Here we will learn how to use custom validation in asp.net with data annotation attributes with example. As we had seen various other validations such as Remote Validation and Data Annotations to validate data now let's create some custom validation because every business application has their own validation rules to be applied.
Generally in asp.net mvc data annotation attributes are used to validate the user inputs ( required , range , Regular expression ..) these are default validation provide by MVC if we want you own Custom Validation then we need to inherit from ValidationAttribute and create it.
After naming it just click on OK. After that a new dialog will popup for selecting template. in that select Basic template and click ok.
After creating application now let's work with Create Model because we are going to use Code first approach of asp.net mvc in this tutorial.
For adding Model just right click on Model folder select Add then inside that select Class in below image you can see the process.
After Clicking on class an Add New item dialog will pop up with class select and asking for class name here we are going to enter class name as "Orders".
After entering class name click on Add button. An order class is added to your project with some default code u can see below code snippet.
After adding Properties we have to add Validation for that we need to create a new class and inherit ValidationAttribute and this class will have the business logic of our custom attribute. As we have added Order class similar way we are going to add another class with name ShippeddateValidate.cs.
Now let's inherit from ValidationAttribute. For that we need to add namespace System.ComponentModel.DataAnnotations.
After inheriting now override the virtual method IsValid and write our custom logic here. Here we are going to validate Shippeddate Property.
In this code snippet I am going to check date selected by user and against current date if date is less than then we will show error message. After compltion of adding custom logic now let's add this custom validation (ShippeddateValidate) to Property.
After completing with adding custom validation (ShippeddateValidate) to Property now let's create controller.
For adding Controller Just Right click on Controller Folder inside that select Add and then select Controller.
After clicking on Controller new dialog will pop up with name Add Controller.
In Template select "Empty MVC controller" and finally click on Add button. Some default code is generated after adding a controller class. Here in this controller code we made some manual configuration by adding Action Selector [HttpGet], [HttpPost] with a New Action Method Index with Orders model parameter as input.
For Adding View just right click inside Index ActionResult Method and Select "Add View" to create the view template for our Index form. Here in below snapshot we selected View engine as Razor and we are going to create a strongly type view for that I have selected Model class Orders and we want to create an input form for that I have selected Create in Scaffold template finally click on Add button.
Now just run application by entering URL http://localhost:####/order/index to access page. Output of our custom validations application before entering validate data.
Output after entering Invalid date (here we entered one day less that current date its validating using Custom validation which we have created and showing error message).
Our output if we selected valid date now it's not showing custom error message its working fine.