Hello,
this is my code I have setup:
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, thus I am updating my database with null values....crash...bom...bang...
Can you help me please?
Comments: There is no difference between calling FluentValidationModelValidatorProvider.Configure(); and the other lines of code. They both do the same thing. If you're running on ASP.NET MVC 3, then yes, you should be using FluentValidation.MVC3. If you're running on MVC 4, then use FluentValidation.MVC4. FluentValidation doesn't add any specific data- attributes - it just uses the standard MVC ones, so if you define a NotNull rule in FluentValidation, then the standard MVC attribute for a required rule should be output in the source. If this is missing, then it means that the validator can't be found. Have you correctly hooked up the class to its validator using a [Validator(typeof(MyValidator))] attribute? Should look something like this: [Validator(typeof(MyModelValidator))] public class MyModel { } public class MyModelValidator : AbstractValidator<MyModel> { //etc }
this is my code I have setup:
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, thus I am updating my database with null values....crash...bom...bang...
Can you help me please?
Comments: There is no difference between calling FluentValidationModelValidatorProvider.Configure(); and the other lines of code. They both do the same thing. If you're running on ASP.NET MVC 3, then yes, you should be using FluentValidation.MVC3. If you're running on MVC 4, then use FluentValidation.MVC4. FluentValidation doesn't add any specific data- attributes - it just uses the standard MVC ones, so if you define a NotNull rule in FluentValidation, then the standard MVC attribute for a required rule should be output in the source. If this is missing, then it means that the validator can't be found. Have you correctly hooked up the class to its validator using a [Validator(typeof(MyValidator))] attribute? Should look something like this: [Validator(typeof(MyModelValidator))] public class MyModel { } public class MyModelValidator : AbstractValidator<MyModel> { //etc }