Databases Reference
In-Depth Information
The
RegularExpressionValidator
is configured by the
SetUpRegexValidator
method
defined in the code-behind. This method first searches the list of attributes applied to
the column in the data model,
Column.Attributes
, for a
RegularExpressionAttribute
that might have been supplied by the application developer to indicate the expected
phone number pattern. If no such attribute exists, this method creates a default
Regular
ExpressionAttribute
with the pattern,
\(\d{3}\)\d{3}-\d{4}
, that matches U.S. phone
numbers, such as (877)514-9180, with a three-digit area code in parentheses, followed by a
seven-digit local number. The default attribute also has a generic error message that says
that a column must be a valid phone number.
Having obtained the
RegularExpressionAttribute
instance, the
SetUpRegexValidator
method configures the
RegularExpressionValidator
control consistently with how the
SetUpValidator
method of the
FieldTemplateUserControl
base class does it—the
Text
displayed by the validator itself is always a star symbol; its
ErrorMessage
is reported
through a
ValidationSummary
control on the page and the
ToolTip
contains the complete
error message.
The last step in setting up the
RegularExpressionValidator
is to call the
IgnoreModel
ValidationAttribute
method of the
FieldTemplateUserControl
base class. This allows
the
DynamicValidator
to ignore the
RegularExpressionAttribute
during its validation
because this attribute is already covered by the
RegularExpressionValidator
. Aside from
helping to avoid doing the same validation check twice, this also prevents the regular
expression validation error from being reported twice—first by the
RegularExpression
Validator
and then by the
DynamicValidator
control.
Listing 3.13 shows an example of how the new
PhoneNumber_Edit
field template can be
associated with a column using data annotation attributes.
LISTING 3.13
Associating PhoneNumber Template with a Column
using System.ComponentModel.DataAnnotations;
namespace DataModel
{
[MetadataType(typeof(Employee.Metadata))]
partial class Employee
{
public class Metadata
{
[DataType(DataType.Date)]
public object BirthDate;
[DataType(DataType.Date)]
public object HireDate;
[DataType(DataType.PhoneNumber)]
[RegularExpression(@”\(\d{2,3}\)\d{3}-\d{4}”,
ErrorMessage = “The Home Phone must be a valid phone number.”)]