Designing Custom Validation Attributes
In this lesson, we will learn code a custom validation attribute that will perform both server-side and client-side validation.
A custom validation attribute can be implemented by inheriting from the ValidationAttribute
class. Server-side validation code is provided by overriding the methods that perform validation and error message formatting.
Client-side validation requires that our attribute implements the IClientModelValidator
interface whose unique AddValidation
method specifies the data-
attributes to add to the input field. The actual client-side validation code must be provided through a JavaScript validator which is connected to the parser that processes all data-
attributes through a JavaScript adapter.
We will describe all the above steps while implementing an example custom validation attribute: the ExtensionAttribute
.
The ExtensionAttribute
As an example, we will define an attribute that checks the extension of a Web Address. It is useful when combined with the UrlAttribute
to force the user to insert the address of a pdf, or of a supported image type. The basic idea is to pass it a string with all comma-separated allowed extensions. Something like .jpg
,.jpeg
, or .png
. The usage should be similar to the example below:
Get hands-on with 1300+ tech skills courses.