In ASP.NET Dynamic Data you can customize and extend data validation in several different ways.
- Using DataAnnotations attributes on individual data fields.
- Overriding the partial class method that processes changes for the individual data field.
- Override the OnValidating or Validate event, to customize validation for any data field.
- Create a custom validation attribute
Creating a partial class for validation
The first thing we need to do, is to create the partial class that extends the data model. This makes it possible to add metadata through attributes and implementing partial class methods to create your own validation logic. We have already done this in this serie in part 2, so take a look there if you don’t know how to do this, after that we need to create the metadata class which is done in part 3.
Customize validation using attributes
This is the easiest way. It allows you to use default validation rules provided by the DataAnnotations attributes.
- In the partial class create a property or field with the same name as the data field to validate.
- Apply one of the attributes from the DataAnnotations namespace to the field or property.
The example makes the Name attribute required and the length of the description at least 10 characters long. You can also apply several validation attributes to the same field.
Using partial class method for an individual field
Another way to do more complex validation, is to override a partial class method that processes changes made to an individual data field. The naming convention for this is On<FieldName>Changing.
- Override the partial class method.
- Add the validation logic.
The example validates the name is at least 5 characters long, and the price is non-negative. This example could also be done using the built-in attrbutes. All exceptions thrown in the data model are caught by the DynamicValidator control, which also displays it in the page. The parameter is typed to match the data type in the model.
Using partial class method for all fields in the model
This method allows you to control the validation logic for all fields in the data model at the same time, which is very useful when the logic can be applied to more than one data field, and allows you to validate combinations of multiple data fields.
The way to do this is different whether you use a Linq to SQL or Entity Framework data model.
Linq to SQL
- Override the OnValidate method
- Add to validation logic
The example validate that a product only can be in stock, when the amount is greater than zero.
- Create a partial class for the entity class you want to implement custom validation on
- Language specific step
- Add the logic to the partial class method
Custom validation attribute
Let’s you create an attribute you can reuse across projects and/or models for a single data field. The attribute must derived from ValidationAttribute.
- Create a new class
- Add references to System, System.Globalization and System.ComponentModel.DataAnnotations
- Make the class sealed
- Derive the class from ValidationAttribute
- Apply the AttributeUsageAttribute and indicate that the attribute only can be applied to a property or field one time
- Override the IsValid and add the validation logic inside
- Optionally you can override the FormatErrorMessage to perform error-message formatting.
- Apply the attribute just as you would with one of the built-in validation attributes
An example of a attribute could look like this