How to: Validate Data
This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.
You can add validation attributes to properties and persistent classes to enforce validation. WCF RIA Services provides several built-in validation attributes that perform common validation checks, and provides the CustomValidationAttribute attribute to enable you to specify customized validation checks.
The default validation attributes in WCF RIA Services are:
- DateTypeAttribute
- RangeAttribute
- RegularExpressionAttribute
- RequiredAttribute
- StringLengthAttribute
You add the validation attributes to persistent classes in the server project and those validation attributes are propagated to their generated client entity representations. At run time, the validation rules are applied to data from the user. You must add metadata classes to add validation attributes. For more information, see How to: Add Metadata Class.
When developing a Silverlight application with WCF RIA Services and Telerik Data Access, Domain Services automatically generate validation rules using database attributes such as required fields or maximum string length.
To add a validation attribute:
- Add a metadata class for the persistent class, as it is described in the How to: Add Metadata Class topic.
-
On the properties or the persistent class that you want to validate, add the validation attributes that perform the validation. The following example shows the RequiredAttribute, RegularExpressionAttribute and StringLengthAttribute attributes applied to the Customer properties.
[System.ComponentModel.DataAnnotations.MetadataTypeAttribute( typeof( Customer.CustomerMetadata ) )] public partial class Customer { internal sealed class CustomerMetadata { public CustomerMetadata() { } public int CustomerID { get; set; } [System.ComponentModel.DataAnnotations.Required] [System.ComponentModel.DataAnnotations.StringLength( 10 )] public string DrvLicNumber { get; set; } [System.ComponentModel.DataAnnotations.StringLength( 50 )] public string FullName { get; set; } [System.ComponentModel.DataAnnotations.RegularExpression( "abc" )] public string Address { get; set; } // .... } }
<System.ComponentModel.DataAnnotations.MetadataTypeAttribute(GetType(Customer.CustomerMetadata))> Partial Public Class Customer Friend NotInheritable Class CustomerMetadata Public Sub New() End Sub Public Property CustomerID() As Integer <System.ComponentModel.DataAnnotations.Required, System.ComponentModel.DataAnnotations.StringLength(10)> Public Property DrvLicNumber() As String <System.ComponentModel.DataAnnotations.StringLength(50)> Public Property FullName() As String <System.ComponentModel.DataAnnotations.RegularExpression("abc")> Public Property Address() As String ' .... End Class End Class
Build the solution.
- In the Silverlight application, open the generated code file in the Generated_Code folder, and notice the validation attributes that are applied in the client code.
To add custom validation rules:
Sometimes the standard validation attributes don't offer enough flexibility. In these scenarios you need to use the CustomValidation attribute.
- Add a metadata class for the persistent class, as it is described in the How to: Add Metadata Class topic.
- Add a shared code file by using the *.shared.cs naming pattern (e.g. Customer.shared.cs). The code file will contain the custom validation.
-
Add a method that determines whether the data is valid. The method must be public and static. It must return a ValidationResult to indicate the result of the validation check. The following example shows a class named CustomerValidator with a method named IsCustomerValid that validates a Customer object. When the data is not valid you should return an error message and the name of the property that failed the validation.
using System.ComponentModel.DataAnnotations; namespace OA.SL.RIA.Demo.Web { public class CustomerValidator { public static ValidationResult IsCustomerValid( Customer customerToValidate, ValidationContext context ) { if ( customerToValidate.ZIPCode.EndsWith( "Net" ) == false ) return new ValidationResult( "ZipCode must end with 'Net'", new string[] { "ZipCode" } ); else return ValidationResult.Success; } } }
Imports System.ComponentModel.DataAnnotations Namespace OA.SL.RIA.Demo.Web Public Class CustomerValidator Public Shared Function IsCustomerValid(ByVal customerToValidate As Customer, _ ByVal context As ValidationContext) As ValidationResult If customerToValidate.ZIPCode.EndsWith("Net") = False Then Return New ValidationResult("ZipCode must end with 'Net'", New String() { "ZipCode" }) Else Return ValidationResult.Success End If End Function End Class End Namespace
-
On the persistent class or property that you want to validate, add the CustomValidationAttribute attribute, passing the type of the validation object and the name of the method that performs the validation.
[CustomValidation( typeof( CustomerValidator ), "IsCustomerValid" )] [MetadataTypeAttribute( typeof( Customer.CustomerMetadata ) )] public partial class Customer { internal sealed class CustomerMetadata { public CustomerMetadata() { } } }
<CustomValidation(GetType(CustomerValidator), "IsCustomerValid")> <MetadataTypeAttribute(GetType(Customer.CustomerMetadata))> Partial Public Class Customer Friend NotInheritable Class CustomerMetadata Public Sub New() End Sub End Class End Class
Build the solution.