Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

CustomValidationAttribute.Method Property

Gets the name of the method to invoke for validation.

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

public string Method { get; }

Property Value

Type: System.String
The name of the method to invoke for validation.

The method specified in the Method property must be public and static in C#, or Public and Shared in Visual Basic. The specified method must return a ValidationResult object that equals Success if the value is valid, or return an instance of the ValidationResult class that contains an error message if the value is not valid.

The validation method must contain at least one input parameter for the value to validate. The value parameter can be strongly typed. If an object of a different type is passed in the method, the CustomValidationAttribute class attempts to convert the passed value to the specified type before executing the validation method. The method may also contain a ValidationContext object to provide additional information about the validation request. The following examples show the allowed signatures in C# and Visual Basic.

public static ValidationResult MethodName(object value) {...}
public static ValidationResult MethodName(object value, ValidationContext context) {...}

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(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.


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.");
    }
  }
}


Silverlight

Supported in: 5, 4, 3

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

Community Additions

ADD
Show:
© 2015 Microsoft