HOW TO:驗證資料
本主題說明如何將驗證屬性 (Attribute) 加入至屬性 (Property) 和實體,來強制執行驗證規則。WCF RIA Services 提供幾種執行一般驗證檢查的驗證屬性,也提供 CustomValidationAttribute 屬性讓您可以指定自訂驗證檢查。
RIA Services 提供下列的預設驗證屬性:
您可以將驗證屬性加入至伺服器專案中的實體,而驗證屬性會傳播到自己產生的用戶端實體表示。在執行階段,驗證規則會套用至使用者的資料。您必須加入中繼資料類別,以便加入驗證屬性。如需如何執行此作業的詳細資訊,請參閱 HOW TO:加入中繼資料類別。
本主題說明如何加入預設和自訂驗證屬性。
若要加入 RIA Services 所提供的驗證屬性
加入實體類別的中繼資料類別,如 HOW TO:加入中繼資料類別所述。
在要驗證的屬性 (Property) 或實體上加入執行驗證的驗證屬性 (Attribute)。
下列範例將示範套用至名為
AddressLine1
之屬性 (Property) 的 RequiredAttribute 和 StringLengthAttribute (Attribute)。<Required()> _ <StringLength(60)> _ <RoundtripOriginal()> _ Public AddressLine1 As String
[Required] [StringLength(60)] [RoundtripOriginal] public string AddressLine1 { get; set; }
建置 (Ctrl+Shift+B) 方案。
在 Silverlight 應用程式中,開啟 [Generated_Code] 資料夾中產生的程式碼檔案,並注意驗證屬性已套用在用戶端程式碼中。
若要加入自訂驗證屬性
加入實體類別的中繼資料類別,如 HOW TO:加入中繼資料類別所述。
使用
*.shared.cs
或*.shared.vb
命名模式來加入共用程式碼檔案。這個程式碼檔案將包含自訂驗證物件。
加入一個判斷資料是否有效的方法。
此方法必須是 public 和 static (或 Visual Basic 中為 Public 和 Shared)。它必須傳回 ValidationResult 來表示驗證檢查的結果。當您定義自訂驗證類別時,至少必須提供一些程式碼,而不是用戶端專案中正確產生之類別的自動實作屬性。
下列範例將示範一個名為
ProductValidator
類別,具有一個名為IsProductValid
的方法,用來驗證 Product 實體。當資料無效時,會傳回錯誤訊息以及驗證失敗的屬性名稱。Imports System.ComponentModel.DataAnnotations Public Class ProductValidator Public Shared Function IsProductValid(ByVal productToValidate As Product, ByVal context As ValidationContext) If (productToValidate.ListPrice < (CDec(0.8) * productToValidate.StandardCost)) Then Return New ValidationResult("ListPrice is below 80 percent of StandardCost.", New String() {"ListPrice"}) Else Return ValidationResult.Success End If End Function End Class
using System; using System.ComponentModel.DataAnnotations; namespace RIAServicesExample.Web { public class ProductValidator { public static ValidationResult IsProductValid(Product productToValidate, ValidationContext context) { if (productToValidate.ListPrice < ((decimal).8 * productToValidate.StandardCost)) { return new ValidationResult("ListPrice is below 80 percent of StandardCost.", new string[] { "ListPrice" }); } else { return ValidationResult.Success; } } } }
在要驗證實體或屬性 (Property) 上加入 CustomValidationAttribute 屬性 (Attribute),並傳遞驗證物件的型別和執行驗證的方法。
下列範例將示範 CustomValidationAttribute 屬性套用至實體。驗證物件型別是
ProductValidator
,而方法是IsProductValid
。<CustomValidation(GetType(ProductValidator), "IsProductValid")> _ <MetadataTypeAttribute(GetType(Product.ProductMetadata))> _ Partial Public Class Product Friend NotInheritable Class ProductMetadata 'Metadata classes are not meant to be instantiated. Private Sub New() MyBase.New() End Sub Public Color As String Public DiscontinuedDate As Nullable(Of DateTime) Public ListPrice As Decimal Public ModifiedDate As DateTime Public Name As String Public ProductCategoryID As Nullable(Of Integer) Public ProductID As Integer Public ProductModelID As Nullable(Of Integer) Public ProductNumber As String Public rowguid As Guid Public SellEndDate As Nullable(Of DateTime) Public SellStartDate As DateTime <Required()> _ <StringLength(20)> _ Public Size As String Public StandardCost As Decimal Public ThumbNailPhoto() As Byte Public ThumbnailPhotoFileName As String Public Weight As Nullable(Of Decimal) End Class End Class
[CustomValidation(typeof(ProductValidator), "IsProductValid")] [MetadataTypeAttribute(typeof(Product.ProductMetadata))] public partial class Product { internal sealed class ProductMetadata { // Metadata classes are not meant to be instantiated. private ProductMetadata() { } public string Color; public Nullable<DateTime> DiscontinuedDate; public decimal ListPrice; public DateTime ModifiedDate; public string Name; public Nullable<int> ProductCategoryID; public int ProductID; public Nullable<int> ProductModelID; public string ProductNumber; public Guid rowguid; public Nullable<DateTime> SellEndDate; public DateTime SellStartDate; [Required()] [StringLength(20)] public string Size; public decimal StandardCost; public byte[] ThumbNailPhoto; public string ThumbnailPhotoFileName; public Nullable<decimal> Weight; } }
建置 (Ctrl+Shift+B) 方案。
在 Silverlight應用程式中,開啟 [Generated_Code] 資料夾。請注意,共用程式碼檔案存在於這個資料夾中,以及 CustomValidationAttribute 如何套用至實體。