Validation is the most common business rule on a database. Each DataField needs
to have constraints on the data it accepts and validation supplies those constraints.
For example, an integer DataField for Age may be constrained to values from 0 to
100.
The class System.ComponentModel.DataAnnotations.ValidationAttribute
is the base class for all ValidationAttributes. Its subclasses define properties
specific to each validation rule. For example, the RangeAttribute validates a range
with properties for Minimum and Maximum. Many DataTypeAttributes are also ValidationAttributes,
providing validation for strings that need to be converted to another data type.
BLD supplies many ValidationAttribute classes, including some that implement the
same validation rules that Microsoft supplies. BLD's attributes have more features,
including better handling of error messages, runtime customization, and a way to enable them based on values
of other columns.
Here are some ValidationAttributes associated with the Employee Entity class.
public class EmployeeMetadata
{
[DESDA.Required()]
[DESDA.RegularExpression(@"\S\s+\S", CaseInsensitive=true,
ErrorMessage="Enter a building number and street name.")]
public object Address { get; set; }
[DESDA.DateDataType()]
[DESDA.Required()]
[DESDA.Range()]
public object BirthDate { get; set; }
[DESDA.StringLength(4)]
public object Extension { get; set; }
}
Here are the ValidationAttribute classes available in BLD.
Attribute class
|
Usage
|
DESDA.CharacterSetAttribute
|
Use on string DataFields that restrict the character set. On Field Templates that
use the FilteredTextBox for data entry, this attribute also sets up the character
set rules of the FilteredTextBox.
|
DESDA.CompareToValueAttribute
|
Compare the value of this DataField to a value assigned to the ValueToCompare property.
|
DESDA.CompareTwoColumnsAttribute
|
Compares the value of this DataField with another DataField.
|
DESDA.DifferenceAttribute
|
Compare the value of two DataFields to determine how the difference between their
values compares to a value in the DifferenceValue property.
|
DESDA.EmailDomainsValidationAttribute
|
Use with an email address to either require or invalidate specific domains. It compliments
the DESDA.EmailAddressDataTypeAttribute.
|
DESDA.FileExtensionValidationAttribute
|
Use when evaluating a Url that must have a specific file extension or extensions.
It compliments the DESDA.UrlDataTypeAttribute.
|
DESDA.RangeAttribute
|
Establish a range.
|
DESDA.RegularExpressionAttribute
|
Match a string DataField value to a specific pattern using a regular expression.
|
DESDA.RequiredAttribute
|
Require a value in the DataField.
|
DESDA.StringLengthAttribute
|
Impose limits on the number of characters permitted in a string DataField.
|
DESDA.StringListAttribute
|
Compares a string to a list of strings to see if there is a match.
|
DESDA.SpecialDatesAttribute
|
Defines a list of dates and associated data. Validates unselectable dates as invalid and provides both selectable and unselectable dates to the user interface layer for presentation.
|
DESDA.SpecialTimesAttribute
|
Defines a list of times and associated data. Validates unselectable times as invalid and provides both selectable and unselectable times to the user interface layer for presentation.
|
DESDA.CustomValidationAttribute
|
When no other ValidationAttribute applies.
|
Remember that many DataTypeAttributes also provide validation. (In fact, System.ComponentModel.DataAnnotations.DataTypeAttribute
inherits from System.ComponentModel.ValidationAttribute.) For example, when you add
a CreditCardNumberAttribute, you also get validation that ensures the number entered matches the Luhn Algorithm.
The Source Code Browser shows completed DataAnnotations. The ValidationAttributes
have been highlighted.
In the next topic, you'll learn how to
customize ValidationAttributes at runtime.
Open the Source Code Browser (C# only)