Enum Conversion Validator
Class Name: EnumConversionValidator
Attribute Name: EnumConversionValidatorAttribute
Configuration tool name: Enum Conversion Validator
Description
This validator checks that a string can be converted to a value in a specified enum type. For example, it can check that "Blue" can be converted to a value in the Color enumeration.
Properties
The following table lists the enum conversion validator properties. The actual property names shown in the configuration tools are listed in the table description.
Property |
Definition |
---|---|
EnumType |
Enumeration Type - This property is the type of the enumeration. You can either enter the type or you can select it with the type selector dialog box. |
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 |
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 instead of when it is not met. The default is False. |
Tag |
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. |
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 following table lists the tokens that are supported by the enum conversion validator.
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. |
{3} |
The type of enumeration to which the validator will attempt to convert. |
Examples
The following code examples show how to use the validator with attributes and with code.
Using Attributes
The following code example checks that the sales unit is a string that can be converted to a value of the Unit enumeration. For example, the string "Each" is allowed.
public class Product
{
enum Unit
{
Each, Dozen, Gross
}
[EnumConversionValidator(typeof(Unit))]
public string SalesUnit
{
get
{
return salesUnit;
}
}
// ...
}
'Usage
Public Class Product
Enum Unit
_Each
_Dozen
_Gross
End Enum
<EnumConversionValidator(GetType(Unit))> _
ReadOnly Property SalesUnit(ByVal _salesUnit As String)
Get
Return _salesUnit
End Get
End Property
End Class
Using Code
The following code example constructs an EnumConversionValidator instance that can be used to check whether the sales unit is a string that is convertible to a value of the Unit enumeration. For example, the string "Each" would be allowed.
public class Product
{
enum Unit
{
Each, Dozen, Gross
}
Validator unitValidator = new EnumConversionValidator(typeof(Unit));
// ...
}
'Usage
Public Class Product
Enum Unit
_Each
_Dozen
_Gross
End Enum
Dim unitValidator As Validator = New EnumConversionValidator(GetType(Unit))
' ...
End Class