Export (0) Print
Expand All

ValidationResult Class

Contains the results of a validation request.

System.Object
  System.ComponentModel.DataAnnotations.ValidationResult

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

public sealed class ValidationResult

The ValidationResult type exposes the following members.

  NameDescription
Public methodValidationResult(String)Initializes a new instance of the ValidationResult class with the specified error message.
Public methodValidationResult(String, IEnumerable<String>)Initializes a new instance of the ValidationResult class with the specified error message and a collection of member names that are associated with the validation result.
Top

  NameDescription
Public propertyErrorMessageGets or sets the error message for the validation result.
Public propertyMemberNamesGets the collection of member names associated with the validation result.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string value that represents the current validation result. (Overrides Object.ToString().)
Top

  NameDescription
Public fieldStatic memberSuccessA value that indicates the entity member successfully validated.
Top

The ValidationResult class stores the outcome of a validation check. The IsValid and GetValidationResult methods return an instance of the ValidationResult class, which contains values to indicate whether the value of the entity member matches the validation attributes applied to that member.

If the value of the member successfully validates, the returned ValidationResult object equals the value of the Success field. To determine whether validation succeeded, you should check whether the returned object equals Success. If the value of the member does not validate, the returned ValidationResult object contains an error message and a collection of member names for the validation error, if they can be retrieved.

The following example shows how to return a validation result that indicates success or failure.


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.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

ADD
Show:
© 2015 Microsoft