How to: Customize Data Field Validation in the Data Model Using Custom Attributes
ASP.NET Dynamic Data enables you to customize and extend data field validation at the data layer level. This topic shows how you can add data field validation in the data model by doing the following:
-
Creating a custom validation attribute. This attribute enables you to create custom metadata that you use in the data model for validation.
-
Applying the custom validation attribute. After the custom attribute is created, you apply it to the data fields that you want to validate.
See a run-time code example of this feature: Run.
A custom validation attribute lets you create metadata that you can use in the data model to validate data fields. You must derive the custom attribute from the ValidationAttribute base class.
To create a custom validation attribute
-
In Solution Explorer, right-click the App_Code folder, and then click Add New Item.
-
Under Add New Item, click Class.
In the Name box, enter the name of the custom validation attribute class. You can use any name that is not already being used. For example, you can enter the name CustomAttribute.cs in Visual C# or CustomAttribute.vb in Visual Basic in order to create a custom attribute class named CustomAttribute.
-
Add references to the System, System.Globalization, and System.ComponentModel.DataAnnotations namespaces by using the Imports keyword in Visual Basic or the using keyword in Visual C#, as shown in the following example:
-
Make the following changes to the class definition:
-
Make the class not inheritable. Add the NotInheritable keyword in Visual Basic or the sealed keyword in Visual C#.
-
Derive the class from the ValidationAttribute base type.
-
Apply the AttributeUsageAttribute attribute to the class definition to state how your custom validation attribute must be used.
The following example shows a class definition. The AttributeUsageAttribute parameters are set so that the custom validation attribute can be applied to properties or fields only one time.
-
-
Override the IsValid method and add logic to perform validation. Return true if the custom validation is successful, or false if it fails. The value to be validated is passed to the method as the only parameter.
The following example shows the overridden method.
-
Optionally, override the FormatErrorMessage method to perform custom error-message formatting.
The following example shows how to use the name of the data field that failed the validation to build a custom error message. The ErrorMessageString value is passed as a parameter when the custom attribute is applied to the data field.
-
Save and close the class attribute file.
To customize validation for a data field you must implement a partial class that extends the data model. This enables you to apply the custom attribute to the data field.
To create a partial class for validation
-
In Solution Explorer, right-click the App_Code folder, and then click Add New Item.
-
Under Visual Studio installed templates, click Class.
In the Name box, enter the name of the database table you want to add validation for.
In the Name box, enter the name of the database table that you want to add validation for. For example, to add validation for the Customers table, enter "Customer" and name the class Customer.
-
Add the Partial keyword in Visual Basic or the partial keyword in Visual C# to the class definition to make it a partial class.
-
If you are creating the class in Visual C#, delete the default constructor.
The following example shows the updated class declaration.
-
Add references to the System.Web.DynamicData and System.ComponentModel.DataAnnotations namespaces by using the Imports keyword in Visual Basic or the using keyword in Visual C#, as shown in the following example:
-
In the same file, create a second class that will act as the associated metadata class. You can use any name for the class that is a valid class name and that is not already being used.
The following example shows a metadata class declaration.
The associated metadata class provides an object that you can apply validation attributes to.
-
Apply the MetadataTypeAttribute attribute to the partial class definition. For the attribute's parameter, use the name of the associated metadata class that you created in the previous step.
The following example shows the partial-class definition with the attributed added.
You can now apply the custom validation attribute to a data field.
To apply a custom validation attribute to a data field
-
In the metadata class, create a property or field whose name corresponds to the data field to validate.
-
Apply the custom validation attribute that you created earlier to the data field you that you want to validate.
The following example shows how to apply the custom validation attribute to the Phone data field.
-
Save and close the class file.
The following example shows how to create and apply a custom attribute named PhoneMaskAttribute to the Phone data field of the Customer table in the AdventureWorksLT database. The example uses a LINQ-to-SQL class for the data model.
The attribute instructs Dynamic Data to validate the Phone data field against a mask that represents a specific telephone number format. If the telephone number entered by the user does not match the mask, the attribute code issues a custom error.
To compile the example code, you need the following:
-
Microsoft Visual Studio 2008 Service Pack 1 or Visual Web Developer 2008 Express Edition Service Pack 1.
-
The AdventureWorksLT sample database. For information about how to download and install the SQL Server sample database, see Microsoft SQL Server Product Samples: Database on the CodePlex site. Make sure that you install the correct version of the sample database for the version of SQL Server that you are running (Microsoft SQL Server 2005 or Microsoft SQL Server 2008).
-
A dynamic data-driven Web site. This enables you to create a data context for the database and to create the class that contains the data field to customize and the methods to override. For more information, see Walkthrough: Creating a New Dynamic Data Web Site using Scaffolding.