2012-04-13 11 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

モデルを受信するコントローラはどのように見えますか? –

+1

私は空のMVCプロジェクトであなたのコードを試して、IsValidの呼び出しが必要なように起こります。 – Iridio

+0

'登録'タイプのモデルが使用されている場合、 'アクション'メソッドを提供しますか?私はあなたのコードをテストしました、それはサーバー側で働いています。 'AgeVAttribute'はIClientValidatableを実装していないので、クライアント側の検証はオフになっています。 – Kibria

答えて

2

缶't repro。

モデル:

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(登録モデル) { リターンビュー(モデル); } IsValidメソッドが実行されました。それが困難でない場合は、あなたの助けを借りてサーバサイドのおかげではなく、クライアント側の検証を使ってチェックを行う方法を教えてください。 – Ben

+0

カスタム検証属性に 'IClientValidatable'インターフェイスを実装し、カスタムアダプタを登録することができますこれはサーバー上にある同じ検証ロジックを記述して複製する必要があるjavascript関数を表します。ここに例があります:http://stackoverflow.com/a/4747466/29407 –

+0

こんにちはダーリン、私があなたが投稿した例をチェックしました。私が最後の部分(カスタムアダプターを定義する)まで私はあなたに私を見せてくださいどのように年齢制限(カスタムアダプター)を実装すると、私はsee.test()メソッドの例を見て、何をしているのかわからない。 – Ben