The idea to use data
annotation is to add constraint meta data to properties of the view model. The meta data then picked up by default
binder when executes the model binding process and used to include additional validation
to the data in the form field.
By adding constraints meta data to view
model and allowing default binder to do validation as part of model binding
process, we get certain benefits that is given below:
• The
validation rule is enforced across whole application and not just single action
method.
• We
do not have to call method, instantiate class or do anything for the validation
rules that need to be enforced, default binder will do this automatically.
• The
amount of codes that required to implement validation rules for annotations is
considerably very less than when do it
manually.
Now modify the code to see data
annotations in action. 1st, remove manually added validations and make action
method then we will accomplish following tasks:
• Make
Form's Email and Message fields required.
• Set
maximum length of the From field to 100 characters which is the length defined
in the database field. Same constraint will be applied to Email and Subject
fields.
• Set
maximum length of the Message field to 2000 characters.
• Ensure
that the email address is formatted properly.
• Specify
the custom error message for all the annotations for further helping user to
fix errors.
The result is view model which shown in
Figure 1. Note that all data annotations allow us to specify custom error
message with ErrorMessage
property.
Figure 1. Adding Data Annotations to
the View Model
There are few other annotations that we
can use to enforce validation rule in view model. Some will work at client
browser and give the user immediate feedback and opportunity to fix entries
before sending them to server.
Data Annotations List:
[CreditCard] => It defines the value as a credit card
number which can have 15 or 16 digits without spaces or dashes.
[EmailAddress] => It indicates that the value must be well
formatted email address.
[EnumDataType(typeof(name of enum))]
=> It enables
.NET Framework enumeration that can be mapped to a data column. Its value must
be one which is defined in enumeration.
[FileExtension(Extensions="")]
=> It validates
the file extension of uploaded file which should be in the acceptable defined
extensions. For example [FileExtensions(Extensions=".jpg,.png,.gif")].
[Range(min, max)] => It indicates that the value must be between
specified minimum and
maximum range.
[RegularExpression(pattern)] =>
It indicates that the
value must match specified pattern (regular expression).
[Required] => It indicates that the field must contain a
value. The validation exception will be raised if the field is null or contains
empty string (blank) or contains only white space characters. If we want to
treat white pace characters as a valid character then we need to use [Required(AllowEmptyStrings=true)].
[StringLength(max)] => It indicates the maximum character length
of the value can have and optionally, the minimum character length. Type of
this properties should be string.
Now if we execute the
application, we can see different type of errors in the browser
(client side code) as shown
in Figure 2.
Figure 2. Validation errors of the form
fields based on view model annotations
No comments:
Post a Comment