2014-07-01 56 views
1

我的模型類:FluentValidation斷言驗證不起作用

[FluentValidation.Attributes.Validator(typeof(CrcValidator))] 
public class CrcModel 
{ 
    [Display(Name = "Binary value")] 
    public string binaryValue { get; set; } 

    [Display(Name = "Generator")] 
    public string generator { get; set; } 
} 

和校驗類斷言:

public class CrcValidator : AbstractValidator<CrcModel> 
{ 
    public CrcValidator() 
    { 
     RuleFor(x => x.binaryValue) 
      .NotEmpty().WithMessage("Binary value is required") 
      .Matches(@"(0|1)*").WithMessage("This value is not valid binary value"); 

     RuleFor(x => x.generator) 
      .NotEmpty().WithMessage("Generator is required") 
      .Matches(@"(0|1)*").WithMessage("Generator must be valid binary value") 
      .Must(CompareLength).WithMessage("Length must be lesser than length of binary value - 1"); 
    } 

    private bool CompareLength(CrcModel model, string value) 
    { 
     return model.binaryValue.Length - 1 > model.generator.Length; 
    } 
} 

我把CompareLength函數和每個值內斷點正確地從構成讀。問題是即使我的謂詞函數返回false,我的表單也會通過驗證。 NotEmpty和Matches規則完美地工作得很好只好似乎被忽視。

編輯

的jQuery的提交(類型爲 「按鈕」)按鈕:

$(function() { 
$("#Button1").click(function() { 
    var form = $("#Form1"); 
    if ($(form).valid()) { 
     $.ajax({ 
      type: 'POST', 
      url: 'Compute', 
      data: $(form).serialize(), 
      success: function (result) { 
       $("#remainder").val(result.remainder); 
       $("#signal").val(result.signal); 
      } 
     }); 
    } 
}); 
}); 

控制器動作處理表單提交:

從必由之路
[HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Compute([Bind(Include = "binaryValue,generator")] CrcModel model) 
    { 
     if (ModelState.IsValid) 
     { 
      model.remainder = ComputeFrame(model.binaryValue, model.generator); 
      model.signal = model.binaryValue + model.remainder; 
     } 

     return Json(new { remainder = model.remainder, signal = model.signal }); 
    } 

驗證規則適用於服務器一面但消息不顯示。

回答

1

編輯:經過幾次評論,所有在FluentValidation中對Must的調用中定義的驗證僅在服務器上執行。沒有JavaScript等價於任意C#代碼。 C#中的FluentValidation規則被MVC框架轉換爲HTML中表單字段標籤上的各種HTML5 data-*屬性,jQuery Validate在客戶端驗證期間讀取這些屬性。由於無法將謂詞(委託方法)中定義的驗證規則轉換爲HTML5 data-*屬性,因此這些驗證規則只能在服務器端強制執行。

編輯#2:看到作爲Must驗證由AJAX調用觸發,你需要做兩件事情:

  1. HTTP狀態代碼設置爲觸發非-200響應jQuery中的錯誤處理機制。另外,設置HTTP狀態422(無法處理的實體)

    HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Compute([Bind(Include = "binaryValue,generator")] CrcModel model) 
    { 
        if (ModelState.IsValid) 
        { 
         model.remainder = ComputeFrame(model.binaryValue, model.generator); 
         model.signal = model.binaryValue + model.remainder; 
    
         return Json(new { remainder = model.remainder, signal = model.signal }); 
        } 
        else 
        { 
         Response.StatusCode = 422; 
    
         return Json(new { errors = ModelState.Errors }); 
        } 
    } 
    
  2. 更改jQuery.ajax電話:

    $(function() { 
        $("#Button1").click(function() { 
         var form = $("#Form1"); 
         if ($(form).valid()) { 
          $.ajax({ 
           type: 'POST', 
           url: 'Compute', 
           data: $(form).serialize(), 
           success: function (result) { 
            $("#remainder").val(result.remainder); 
            $("#signal").val(result.signal); 
           }, 
           error: function(xhr) { 
            if (xhr.status == 422) { 
             var errors = JSON.parse(xhr.responseText); 
    
             console.log(errors); 
            } 
           } 
          }); 
         } 
        }); 
    }); 
    

另一種是隻呈現部分顯示一個或更多錯誤消息爲HTML,然後將該HTML設置爲DIV並顯示DIV標記。

+0

不幸的是,這也不起作用。 – Cheerup

+1

嗯。奇怪。我已經在ASP.NET MVC 4中使用過這種類型的東西,它工作正常。所以'ModelState.IsValid'報告'真'? –

+0

哦,親愛的,其實我忘了在我的控制器中檢查ModelState.IsValid,我假設如果給定的消息沒有顯示,則謂詞不起作用。使用ModelState.IsValid驗證從謂詞工作更新我的控制器後,確實仍然不顯示消息。任何想法可能是消息的問題? – Cheerup