Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Specifying Type-Level Validation Rules > Using CustomValidationAttributes for T...

Using CustomValidationAttributes for Type Validation

You can also use CustomValidationAttribute on a type rather than an individual property, allowing you to define a validation that takes into account more than a single property. We’ll show you how you can define the same validation in the IValidatableObject example above by using CustomValidationAttribute.

The signature of a CustomValidationAttribute has the target type specified in the parameter along with a ValidationContext, which is used in the same way as the ValidationContext parameter of the Validate method. Example 6-12 shows two validation methods that are added into the Trip class. Notice that these methods are both public and static. Also notice that we’re using separate methods for each validation. That’s because a ValidationAttribute can only return a single ValidationResult.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial