This topic has not yet been rated - Rate this topic

ValidationAttribute Class

Serves as the base class for all validation attributes.

Namespace:  System.ComponentModel.DataAnnotations
Assembly:  System.ComponentModel.DataAnnotations (in System.ComponentModel.DataAnnotations.dll)
public abstract class ValidationAttribute : Attribute

The ValidationAttribute type exposes the following members.

  Name Description
Protected method ValidationAttribute() Initializes a new instance of the ValidationAttribute class.
Protected method ValidationAttribute(Func<String>) Initializes a new instance of the ValidationAttribute class with the specified function to retrieve the error message.
Protected method ValidationAttribute(String) Initializes a new instance of the ValidationAttribute class with the specified error message.
Top
  Name Description
Public property ErrorMessage Gets or sets the non-localizable error message to display when validation fails.
Public property ErrorMessageResourceName Gets or sets the property name on the resource type that provides the localizable error message.
Public property ErrorMessageResourceType Gets or sets the resource type that provides the localizable error message.
Protected property ErrorMessageString Gets the localized or non-localized error message.
Top
  Name Description
Public method Equals Infrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Protected method 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.)
Public method FormatErrorMessage Applies formatting to the error message.
Public method GetHashCode Returns the hash code for this instance. (Inherited from Attribute.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method GetValidationResult Determines whether the specified object is valid and returns an object that includes the results of the validation check.
Protected method IsValid Determines whether the specified object is valid.
Public method Match When overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Public method Validate Determines whether the specified object is valid and throws a ValidationException if the object is not valid.
Top

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 : ValidationAttribute
{
    public EvenNumberAttribute() : base(() => Resource1.EvenNumberError) { }
    public EvenNumberAttribute(string errorMessage) : base(() => errorMessage) { }

    protected override ValidationResult IsValid(object value, 
        ValidationContext validationContext)
    {
        if (value == null)
        {
            return ValidationResult.Success;
        }

        int convertedValue;
        try
        {
            convertedValue = Convert.ToInt32(value);
        }
        catch (FormatException)
        {
            return new ValidationResult(Resource1.ConversionError);
        }

        if (convertedValue % 2 == 0)
        {
            return ValidationResult.Success;
        }
        else
        {
            return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
        }
    }
}


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.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ