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.
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.
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.
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:
- Custom Data Annotation attribute to validate Age or Integer fields
- Custom Data Annotation attribute to validate Password
- Custom Data Annotation attribute to validate Email Address
- Custom Data Annotation attribute to validate string inputs like name