2012-07-13 36 views
4

我創建了誕生這樣的日期驗證屬性:數據anntotation屬性。如何設置執行順序?

public class DateRequired : ValidationAttribute 
    { 
     public override bool IsValid(object value) 
     { 
      DateTime date = Convert.ToDateTime(value); 
      return date != DateTime.MinValue; 
     } 
    } 

    public class DateGraterThanEighteen : ValidationAttribute 
    { 
     public override bool IsValid(object value) 
     { 
      DateTime date = Convert.ToDateTime(value); 
      long ticks = DateTime.Now.Ticks - date.Ticks; 
      int years = new DateTime(ticks).Year; 
      return years >= 18; 
     } 
    } 

I applied attributes like this 

     [DateGraterThanEighteen(ErrorMessage="You must be at least 18")] 
     [DateRequired(ErrorMessage = "Date of birth is required")] 
     public DateTime DateOfBirth { get; set; } 

不管我申請的順序,DateGraterThanEighteen先執行。我怎樣才能讓DateRequired先執行?

回答

0

您可以將訂單應用於過濾器。不知道你是否能驗證屬性嘗試

[DateGraterThanEighteen(ErrorMessage="You must be at least 18"), Order=1] 
[DateRequired(ErrorMessage = "Date of birth is required"), Order=2] 
public DateTime DateOfBirth { get; set; } 

僅供參考,您拼錯了更大的:)

更新:沒關係,能不能做到這一點。你需要創建一個ModelValidationProvider