Major benefits of using data
annotations is that we can create our own custom annotations. It allows us to
reuse the validation logic in the different view models. It also allows us to
keep the validation logic in centralized place to make it easier to maintain.
To create custom annotation, we need to
create a class which inherits from ValidationAttribute and name of that class must end with
suffix Attribute. For example, suppose we want to
validate the name entered in From field is a full name (i.e. first name and
last name available or not). Below steps will guide us through the process to
create the custom data annotation:
1. Create one new class in Model directory and give name FullNameAttribute.
2. Include System.ComponentModel.DataAnnotations
name space at top of
the file
with rest of the default name spaces.
3. Make FullNameAttribute class inherit from class ValidationAttribute.
4. Override method IsValid.
5. Add
a validation logic to identify the value passed contains two words or
not. It can be done by splitting the string by spaces as separator. The Split() method of String class return s an
array of strings. So, basically all we need to verify if array has length of 2
or not. If it has length 2 then return true
otherwise return false.
The code in Figure 1 shows result of
previous steps to create a custom data annotation:
Figure 1. Custom Data Annotation to Validate Full
Name Has First and Last Names
We can use above custom data annotation
in our view model. Add it to From
property with an
error message. So, that user knows what the error is (as given below):
If we run the application,
we will see different types of messages depending on the errors found. Example
shown in Figure 2 below:
Figure 2. Validation error from the custom data
annotation
Data validation process is integral
part of the application. As a developer, we need to make sure that the data
sent by user is valid. So that the application work properly. Validation
process given in ASP.NET MVC can be implemented in many different ways but the
simple approach is to use data annotations.
When we
implement the manual approach then we are responsible to implement the
logic for which data should be analyzed and approved. It can be huge task and
normally involves creating lot of code in action method to verify the data in
view model is valid. This approach is not only prone to errors but also very
hard to maintain and the validation logic can not be reused.
Using the data annotations is much better
approach as the constraints are implemented using meta data directly in view
model. The validation logic can be reused through many action methods. Also, by
moving validation logic to view model instead of action method, the validation
happens whenever the view model is used in entire application.
ASP.NET MVC supply a considerable
number of validations attribute that we can use in our view model. But if none
of them fits then we need it and we create our own custom data annotations by
creating the classes that inherits from class ValidationAttribute and implement suitable IsValid method.
No comments:
Post a Comment