ValidationAttribute Class
Serves as the base class for all validation attributes.
System.Attribute
System.ComponentModel.DataAnnotations.ValidationAttribute
System.ComponentModel.DataAnnotations.CustomValidationAttribute
System.ComponentModel.DataAnnotations.DataTypeAttribute
System.ComponentModel.DataAnnotations.RangeAttribute
System.ComponentModel.DataAnnotations.RegularExpressionAttribute
System.ComponentModel.DataAnnotations.RequiredAttribute
System.ComponentModel.DataAnnotations.StringLengthAttribute
Namespace: System.ComponentModel.DataAnnotations
Assembly: System.ComponentModel.DataAnnotations (in System.ComponentModel.DataAnnotations.dll)
The ValidationAttribute type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | ValidationAttribute | Initializes a new instance of the ValidationAttribute class. |
![]() | ValidationAttribute(Func(Of String)) | Initializes a new instance of the ValidationAttribute class with the specified function to retrieve the error message. |
![]() | ValidationAttribute(String) | Initializes a new instance of the ValidationAttribute class with the specified error message. |
| Name | Description | |
|---|---|---|
![]() | ErrorMessage | Gets or sets the non-localizable error message to display when validation fails. |
![]() | ErrorMessageResourceName | Gets or sets the property name on the resource type that provides the localizable error message. |
![]() | ErrorMessageResourceType | Gets or sets the resource type that provides the localizable error message. |
![]() | ErrorMessageString | Gets the localized or non-localized error message. |
| Name | Description | |
|---|---|---|
![]() | Equals | Infrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.) |
![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.) |
![]() | FormatErrorMessage | Applies formatting to the error message. |
![]() | GetHashCode | Returns the hash code for this instance. (Inherited from Attribute.) |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | GetValidationResult | Determines whether the specified object is valid and returns an object that includes the results of the validation check. |
![]() | IsValid | Determines whether the specified object is valid. |
![]() | Match | When overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.) |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
![]() | Validate | Determines whether the specified object is valid and throws a ValidationException if the object is not valid. |
You create a class that derives from the ValidationAttribute class when you need to create a customized validation attribute. You override the IsValid method to provide the logic for your customized validation check.
The Validate method calls the IsValid method and raises a ValidationException if the value is not valid. The GetValidationResult method calls the IsValid method and returns an instance of the ValidationResult class to indicate the result of the validation check.
The following example shows a customized validation attribute that derives from ValidationAttribute. The class overrides the IsValid method to test whether the value is valid. By default, the attribute displays an error message that is included as a resource in the application.
Public Class EvenNumberAttribute Inherits ValidationAttribute Public Sub New() MyBase.New(Function() My.Resources.Resource1.EvenNumberError) End Sub Public Sub New(ByVal errorMessage As String) MyBase.New(Function() errorMessage) End Sub Protected Overrides Function IsValid(ByVal value As Object, _ ByVal validationContext As ValidationContext) As ValidationResult If (value = Nothing) Then Return ValidationResult.Success End If Dim convertedValue As Integer Try convertedValue = Convert.ToInt32(value) Catch ex As FormatException Return New ValidationResult(My.Resources.Resource1.ConversionError) End Try If (convertedValue Mod 2 = 0) Then Return ValidationResult.Success Else Return New ValidationResult(FormatErrorMessage(validationContext.DisplayName)) End If End Function End Class
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
