2012-04-13 99 views
1

簡單的自定義的驗證,MVC3簡單的自定義驗證

我的模式和自定義的驗證:

public class Registration 
{ 
    [Required(ErrorMessage = "Date of Birth is required")]   
    [AgeV(18,ErrorMessage="You are not old enough to register")] 
    public DateTime DateOfBirth { set; get; } 
} 

public class AgeVAttribute : ValidationAttribute 
{ 
    private int _maxAge; 

    public AgeVAttribute(int maxAge) 
    { 
     _maxAge = maxAge; 
    } 

    public override bool IsValid(object value) 
    { 
     return false;  <--- **this never gets executed.... what am I missing?** 
    } 
} 

(請參見上面的在線評論)

觀點:

@using (Html.BeginForm()) { 
@Html.ValidationSummary("Errors") 
<fieldset> 
    <legend>Registration</legend> 
    <div class="editor-label"> 
     @Html.LabelFor(model => model.DateOfBirth) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.DateOfBirth)  
    </div> 

    <p> 
     <input type="submit" value="Create" /> 
    </p> 
</fieldset> 
} 
+1

接收模型的控制器是什麼樣的? – 2012-04-13 16:09:19

+1

我已經在一個空白的MVC項目中嘗試了您的代碼,並且發生了IsValid調用,因爲它應該發生。 – Iridio 2012-04-13 16:10:30

+0

您是否會在使用「註冊」類型的模型時提供「操作」方法?我測試了你的代碼,它在服務器端工作。由於您的'AgeVAttribute'沒有實現IClientValidatable,所以客戶端驗證關閉。 – Kibria 2012-04-13 16:33:04

回答

2

燦沒有回報。

型號:

public class Registration 
{ 
    [Required(ErrorMessage = "Date of Birth is required")] 
    [AgeV(18, ErrorMessage = "You are not old enough to register")] 
    public DateTime DateOfBirth { set; get; } 
} 

public class AgeVAttribute : ValidationAttribute 
{ 
    private int _maxAge; 

    public AgeVAttribute(int maxAge) 
    { 
     _maxAge = maxAge; 
    } 

    public override bool IsValid(object value) 
    { 
     return false; 
    } 
} 

控制器:

public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     return View(new Registration 
     { 
      DateOfBirth = DateTime.Now.AddYears(-10) 
     }); 
    } 

    [HttpPost] 
    public ActionResult Index(Registration model) 
    { 
     return View(model); 
    } 
} 

查看:

@model Registration 

@using (Html.BeginForm()) 
{ 
    @Html.ValidationSummary("Errors") 
    <fieldset> 
     <legend>Registration</legend> 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.DateOfBirth) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.DateOfBirth)  
     </div> 
     <p> 
      <input type="submit" value="Create" /> 
     </p> 
    </fieldset> 
} 

提交表單時,IsValid的方法總是擊中。另外請注意,我沒有啓用客戶端驗證,因爲我沒有包含jquery.validate.jsjquery.validate.unobtrusive.js腳本。如果你已經包含了它們,那麼錯誤的可能性是客戶端驗證會阻止你的表單被提交給服務器,在這種情況下,IsValid方法不會被調用是正常的。

+0

thx Darin,一旦我放置了[HttpPost] public ActionResult Index(註冊模型) { return View(model); }運行IsValid方法。如果沒有困難,請告訴我如何使用客戶端驗證進行檢查,而不是使用serverside感謝您的幫助 – Ben 2012-04-13 16:21:01

+0

您可以讓自定義驗證屬性實現'IClientValidatable'接口,然後註冊一個自定義適配器這將代表一個JavaScript函數,您需要編寫和複製服務器上的相同驗證邏輯。這裏是一個例子:http://stackoverflow.com/a/4747466/29407 – 2012-04-13 16:27:21

+0

嗨達林,我已經檢查了你發佈的例子,一切都很清楚,直到我到最後一部分(定義自定義適配器),請你給我看看你將如何實現年齡限制(自定義適配器),我看看我see.test()方法的例子,不知道在做什麼......如果你能解釋我,那真是太棒了再次感謝。 – Ben 2012-04-13 16:54:19