This is my code I have setup with the latest fluent validation library.
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>
}
All my unit tests for the ReleaseViewModelValidator
show green light. Fine.
But less cool is that entering some live data like 31 chars or entering nothing I do not see any client side error message.
Do I still have to include something in my partial view? The ModelState
is also not correct its always true.
Try this
[FluentValidation.Attributes.Validator(typeof(ReleaseViewModelValidator))]
Because in the FluentValidation attributes validator you have specify the actual validator (ReleaseViewModelValidator
) not the ReleaseViewModel