Custom Validation Data Annotation Attribute in Asp.net MVC3



1. We can create custom Data Annotation attribute, same as we use other DataAnnotation attributes.
2. To create custom Validation attribute, we need to inherit ValidationAttribute class.
3. We need to override IsValid() method of ValidationAttribute class.
4. By creating custom logic for validation attribute we can create attribute which will work and check conditions the way we want.

Watch Video



In order to see how to create DataAnnotation Validation attibute and how to use them, we need to have a form and a model class.
We have following form, a register model class and controller methods ready as shown below.

Model Class :





The above is the register model class. We are going to create a form which will contain the properties declared in the model class. And we are going to apply Validation attribute in this model class itself.


View :



This is our form. We are using helper EditorForModel() to create container for all properties of Register class. On submit form is posted to Welcome method in the controller.


Controller :



This is our controller. In the Welcome Action Method where our form data is posted, we are checking whether ModelState is valid or not. If it is valid then we are returning Welcome view else we are returning Index view with register class's object along with model errors.

ValidationAttribute Class :

We are going to create a separate folder to hold our CustomValidationAttribute class as shown below.



Inside the folder we added a class. As we are going to create a Custom validation attribute to validate FirstName property, we name the class file as FirstNameValidator.




In order to create custom validation attribute we need to add reference of ComponentModel.DataAnnotation. We have inherited ValidationAttribute class and override or used its IsValid method. 
           IsValid method accepts two parameter. One is the value of the property from the form and other is the ValidationContext object, which contains information about property.
This IsValid method returns ValidationResult, either success or validation error.
           In the above class, inside first if block we are validating whether user has entered value for FirstName or not. If user has entered the value then we are returning success else we are returning error message.
           In the second if block, we are checking the length of the FirstName whether it is less than 20 letters or not. In this way we can apply as many conditions or validations we want.








In order to use the custom attriute we created, we need to include namespace in our register class as shown above i.e (using CustomValidtionAttributeInMvc3.Validators;). And we will apply FirstNameValidator attribute to FirstName property as shown
above in the same manner we use to do it for other DataAnnotaions property. When you run the application and check it validates the user input accordingly.





This is the Form rendered. We input nothing and submit the form.




We get validation message for FirstName, as we have created and applied custom Validation attribute for FirstName.




Here we get validation message for input greater than 20 letters.


Points To Remember :

1. We can create our own Validation attribute using validationAttribute class and DataAnnotation.
2. We need to inherit ValdiationAttribute class and override its IsValid method.
3. These custom attributes can be used same way as we use other in built DataAnnotation attribute.


You may also like to view:








5 comments:

  1. its not working:((

    ReplyDelete
    Replies
    1. What issue are you facing ? can you elaborate ?

      Delete
  2. It worked for me :) Thanks

    ReplyDelete
  3. I have followed your tutorial to the letter, but it doesn't validate my entity. Are you referencing a file or a default setting you might have overlooked? Because mine is not working, shows no validation whatsoever.

    Thanks for your help.

    ReplyDelete
    Replies
    1. Can you mail me your solution at 20fingers2brains@gmail.com.
      You can watch video, if you have not. The video link is embedded at top in the page.

      Delete