CustomValidationAttribute.ValidatorType Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets the type that contains the method for validating the member.

Namespace:  System.ComponentModel.DataAnnotations
Assembly:  System.ComponentModel.DataAnnotations (in System.ComponentModel.DataAnnotations.dll)

Syntax

'Declaration
Public ReadOnly Property ValidatorType As Type
public Type ValidatorType { get; }

Property Value

Type: System.Type
The type that contains the method specified in the Method property.

Remarks

The type supplied for the ValidatorType property must be marked as public in C# or Public in Visual Basic.

Examples

The first example shows how to apply the CustomValidationAttribute attribute to an entity member named SalesPerson and an entity class named CustomerAddress. The specified method named ValidateSalesPerson is executed when a value is applied to the SalesPerson property and the method named ValidateAddress is executed when values are applied to the CustomerAddress class.

Public Class Customer
  <CustomValidation(GetType(AWValidation), "ValidateSalesPerson")> _
  Public Property SalesPerson As String
    'Implement Get and Set logic
  End Property
End Class

<CustomValidation(GetType(AWValidation), "ValidateAddress")> _
Public Class CustomerAddress
  ' Provide properties.
End Class
public class Customer
{
  [CustomValidation(typeof(AWValidation), "ValidateSalesPerson")]
  public string SalesPerson { get; set; }
}

[CustomValidation(typeof(AWValidation), "ValidateAddress")]
public class CustomerAddress
{
  // Provide properties.
}

The second example shows the validation type and methods for validating the entity member and entity class. The parameters for the validation methods are strongly-typed. ValidateSalesPerson takes a string value containing the name of the sales person to apply to the property. ValidateAddress takes an instance of the CustomerAddress class.

Imports System.ComponentModel.DataAnnotations

Public Class AWValidation
  Public Shared Function ValidateSalesPerson(salesPerson As String) As ValidationResult
    Dim isValid As Boolean

    ' Perform validation logic here and set isValid to true or false.

    If (IsValid) Then
      ValidateSalesPerson = ValidationResult.Success
    Else
      ValidateSalesPerson = New ValidationResult( _
          "The selected sales person is not available for this customer.")
    End If
    End Function

  Public Shared Function ValidateAddress(addressToValidate As CustomerAddress) As ValidationResult
    Dim isValid As Boolean

    ' Perform validation logic here and set isValid to true or false.

    If (IsValid) Then
      ValidateAddress = ValidationResult.Success
    Else
      ValidateAddress = New ValidationResult( _
          "The address for this customer does not match the required criteria.")
    End If
  End Function
End Class
using System.ComponentModel.DataAnnotations;

public class AWValidation
{
  public static ValidationResult ValidateSalesPerson(string salesPerson)
  {
    bool isValid;

    // Perform validation logic here and set isValid to true or false.

    if (isValid)
    {
      return ValidationResult.Success;
    }
    else
    {
      return new ValidationResult(
          "The selected sales person is not available for this customer.");
    }
  }

  public static ValidationResult ValidateAddress(CustomerAddress addressToValidate)
  {
    bool isValid;

    // Perform validation logic here and set isValid to true or false.

    if (isValid)
    {
      return ValidationResult.Success;
    }
    else
    {
      return new ValidationResult(
          "The address for this customer does not match the required criteria.");
    }
  }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.