2012-11-07 75 views
1

這是我用最新的流利驗證庫設置的代碼。fluentvalidation不顯示錯誤或更新Modelstate

public class ReleaseViewModelValidator : AbstractValidator<ReleaseViewModel> 
{ 
    public ReleaseViewModelValidator() { 
    RuleFor(r => r.Name).NotEmpty().Length(1, 30).WithMessage("Name must not be longer than 30 chars."); 
    } 
} 

[FluentValidation.Attributes.Validator(typeof(ReleaseViewModel))] 
public class ReleaseViewModel { public int ReleaseId { get; set; } 

[Remote("ReleaseExists", "Release", ErrorMessage = "This name already exists.")] 
public string Name { get; set; } 
public DateTime CreatedAt { get; set; } } 

的Global.asax:

FluentValidationModelValidatorProvider.Configure(); 

VIEW:

model ILMD.Web.Models.ReleaseViewModel @* Remote Validation*@ <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 
@using (Html.BeginForm("Create", "Release")) 
{ 
<p class="editor-label">@Html.LabelFor(model => model.Name)</p> 
<p class="editor-field">@Html.EditorFor(model => model.Name)</p> 
<p class="editor-field">@Html.ValidationMessageFor(model => model.Name)</p> 
} 

我所有的單元測試爲ReleaseViewModelValidator顯示綠燈。精細。

但不那麼酷的是,輸入一些實時數據,如31個字符或不輸入任何我沒有看到任何客戶端錯誤消息。

我還需要在我的部分視圖中包含一些東西嗎? ModelState也是不正確的,它總是如此。

+0

請問您的表單元素(HTML源代碼)顯示了不顯眼的驗證特性正在呈現(即數據val要求)?你可以顯示錶單元素的渲染輸出嗎? –

回答

1

試試這個

[FluentValidation.Attributes.Validator(typeof(ReleaseViewModelValidator))] 

因爲在FluentValidation屬性驗證您指定實際的驗證(ReleaseViewModelValidator)不是ReleaseViewModel