Share via


Relative Date Time Validator

Class Name: RelativeDateTimeValidator

Attribute Name: RelativeDateTimeValidatorAttribute

Configuration tool name: Relative Date Time Validator

Description

This validator checks that the DateTime value falls within a specified range using relative times and dates.

Properties

The following table lists the relative date time validator properties. The actual property names displayed in the configuration tools are listed in the table description.

Property

Description

LowerBound

Lower Bound - This is the low-range boundary value representing the number of a specific unit of time (such as days) before the date being validated. It must be of type Integer. The compiler checks this requirement if you directly invoke the validator in your code. If you use attributes or configuration, the application block throws an exception at run time if the type is incorrect.

LowerBoundType

Lower Bound Type - This property determines how to evaluate the LowerBound value. Possible values for LowerBoundType are Ignore, Inclusive, and Exclusive. The Ignore value means that the validator ignores the LowerBound value. This is the default. The Inclusive value means that the validator allows values that are equal to the LowerBound value. The Exclusive value means that the validator does not allow values that are equal to the LowerBound value.

LowerUnit

Lower Bound Unit - This property sets the unit of time for the lower boundary. The possible values are None, Second, Minute, Hour, Day, Month, and Year. The default is None.

MessageTemplate

Message Template - This property is a string containing template tokens that the validator replaces with values as it validates the target. Typically, it describes the validation result.

MessageTemplateResourceName

Template Resource Name - If you do not want to use the MessageTemplate property to hard-code a message template (perhaps for internationalization), you can use a template stored in the application resources. You must also specify a MessageTemplateResourceType value. If you include both a MessageTemplate value and a MessageTemeplateResourceName value, the MessageTemplate value takes precedence.

MessageTemplateResourceType

Template Resource Type - The resource type for the template you want to use. If you specify a MessageTemplateResourceName value, you must specify this value.

Name

Name – The name to use for this validator.

Negated

This is a Boolean property. If it is set to True, it changes the validator's behavior so that it will fail if the condition is met, rather than when it is not met. The default is False.

Tag

This property is a user-supplied string. Typically, it is used to sort or categorize validation results.

TypeName

Type Name – The fully qualified name of the type configuration element. This property cannot be edited.

UpperBound

Upper Bound - This is the upper-range boundary value representing the number of a specific unit of time (such as days) after the date being validated. It must be type Integer. The compiler checks this requirement if you directly invoke the validator in your code. If you use attributes or configuration, the application block throws an exception at run time if the type is incorrect.

UpperBoundType

Upper Bound Type - This property determines how to evaluate the UpperBound value. Possible values for UpperBoundType are Ignore, Inclusive, and Exclusive. The Ignore value means that the validator ignores the UpperBound value. Inclusive is the default. The Inclusive value means that the validator allows values that are equal to the UpperBound value. The Exclusive value means that the validator does not allow values that are equal to the UpperBound value.

UpperUnit

Upper Bound Unit - This property sets the unit of time for the upper boundary. The possible values are None, Second, Minute, Hour, Day, Month, and Year. The default is None.

Message Template Tokens

If the message template contains tokens (for example, "{0}"), the validator will replace these tokens with values when the ValidationResult is created. The tokens supported by the relative date time validator are listed in the following table.

Token

Meaning

{0}

This token represents the value of the object that is being validated. Although it can be useful to show the original value as a part of the validation message, you must be careful to avoid injection attacks by escaping any characters that can be used to attack the system that conveys the message to the user.

{1}

This token represents the key of the object that is being validated. When the validator is attached to a member of a type such as a property or a field, the key is set to the member name. When the validator is attached to an object, the key is null and the token is replaced by an empty string.

{2}

This token represents the tag that is specified on the validator instance. If no tag is supplied, the token is replaced by an empty string.

Example

The following example shows how to use the relative date time validator with attributes. It attaches the RelativeDateTimeValidator attribute to the DateOfBirth property and checks to see if the user is 18 years or older.

public class Person
{
  [RelativeDateTimeValidator(-120, DateTimeUnit.Year, -18, DateTimeUnit.Year,
           Ruleset="RuleSetA", MessageTemplate="Must be 18 years or older.")]
  public DateTime DateOfBirth
  {
    get
    {
      return dateOfBirth;
    }
  }
}
'Usage
Public Class Person
  <RelativeDateTimeValidator(-120, DateTimeUnit.Year, -18, DateTimeUnit.Year, _
           Ruleset:="RuleSetA", MessageTemplate:="Must be 18 years or older.")> _
  Public ReadOnly Property DateOfBirth()
    Get
      Return _dateOfBirth
    End Get
  End Property
End Class