Posts

Showing posts with the label Validation

Custom Attribute Validation in ASP.NET Core

Image
This blog is going to explain what is the Custom Attribute Validation in ASP.NET Core. When the built-in verification attributes are not sufficient to handle validation conditions, you can create your own custom validation attributes. Let’s see how to achieve this step by step. Step 1: The following is the WeeklyEmailAttribute.cs class. Here I have implemented the ValidationAttribute class and override the IsValid method. The IsValid method verifies that the IsAgreed property is true and that the email address is empty. If the condition is true, it will issue a validation message. Otherwise, it will return a success verification result. WeeklyEmailAttribute.cs using System.ComponentModel.DataAnnotations; namespace SampleWebApplication.Validation { public class WeeklyEmailAttribute : ValidationAttribute { protected override ValidationResult IsValid( object value , ValidationContext validationContext) { var sample = (Samp...

IValidatableObject: Class-Level Validation in ASP.NET Core

Image
This blog is going to explain what class-level validation is in ASP.NET Core. ASP.NET Core has some built-in validation attributes, such as [Required], [Range], [EmailAddress]. But those verification attributes are not enough all the time. Sometimes we need to validate based on conditions. In that case, you can inherit and validate the IValidatableObject for the class. This validation only works in the class where you implement the IValidatableObject . This example illustrates how to check the text box content based on the checkbox value. That means if the checkbox is checked it should validate whether the text box is empty or not. Let's look at that step by step. Step 1: The following is a SampleModel class. In this class, I implemented IValidatableObject . This class has only two properties, Join and PhoneNumber. Join property is a bool and PhoneNumber is a string. Also, the Validate method is implemented from IValidatableObject. In the method, I have checked whether t...

[Remote] Attribute Validation in ASP.NET Core

Image
This blog is going to explain what Remote Attribute Verification is and how to use it. In this validation, the Controller action is called using the client-side script. This allows you to verify the server-side code without page refresh. This is because the server-side method is called by the jQuery validation method in the ASP.NET core. Let’s look at it step by step. Step 1: jQuery Library You have to make sure you have the following jQuery. As I mentioned earlier, the server-side controller action method is called by jQuery validation. < script src = "~/lib/jquery/dist/jquery.min.js" ></ script > < script src = "~/lib/jquery-validation/dist/jquery.validate.min.js" ></ script > < script src = "~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js" ></ script > Step 2: Model Next step I have created a Model called MovieModel. It has two properties, MovieName and ReleaseYear. To demons...

Model validation: Rerun validation (TryValidateModel) in ASP.NET Core MVC

Image
This article is going to explain how to validate the model manually. If you want to calculate the value of a model and assign it to the model, you can use TryValidateModel to validate if the model is valid or not. Let us see with an example of how TryValidateModel works. The following is SampleViewModel. The [Required] attribute is included for all properties. That is, all properties are mandatory, so it is a must to assign value to them. public class SampleViewModel { [Required] public int MovieID { get ; set ; } [Required] public string Movie { get ; set ; } [Required] public string DirectedBy { get ; set ; } [Required] public int ReleaseYear { get ; set ; } } Next is a controller Index() action method. Here the value is assigned to MovieID and ReleaseYear, but not to DirectedBy and ReleaseYear. public IActionResult Index() { SampleViewModel model = new SampleViewModel() { MovieID =1, ReleaseYear = 2019 ...