Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
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.