开发者

Attribute based validation with fluent validation doesn't seem to work with asp.net-mvc

i followed all of these steps in this tutorial:

Created a validator class

public class ProjectValidator : AbstractValidator<ProjectViewModel>
{
    public ProjectValidator()
    {
        //RuleFor(h => h.Milestone).NotEmpty().WithName("Milestone");
        RuleFor(h => h.Applications).NotNull().WithName("Application");
        RuleFor(h => h.InitiativeName).NotNull().WithName("Business Aligned Priority");
        RuleFor(h => h.BusinessDriverId).NotNull().WithName("Business Driver");
        RuleFor(h => h.FundingTypeId).NotNull().WithName("Funding Type");
        RuleFor(h => h.Description).NotEmpty().WithName("Description");
        RuleFor(h => h.Name).NotEmpty().WithName("Project Name");
        RuleFor(h => h.Sponsors).NotNull().WithName("Sponsors");
    }
}

Put an attribute on my DTO to specific this validtor

[Validator(typeof(ProjectValidator))]
public class ProjectViewModel
{
}

开发者_如何学Cbut after a form post when i go to check the ModelState errors list, the errors i see are coming from the asp.net-mvc default validation.

 public ActionResult UpdateMe(ProjectViewModel entity)
    {
        Project existingProject = this.Repository.Fetch<Project>(entity.Id);

        UpdateProperties(entity, existingProject);
        var allErrors = ModelState.Values.SelectMany(v => v.Errors);
        if (allErrors.Count() > 0)
        {

any suggestions on why its not picking up the fluent. validator ?? I have added an image below of what i see on the gui

Attribute based validation with fluent validation doesn't seem to work with asp.net-mvc

if i call the validator directly in code it works just fine:

 ProjectValidator validator = new ProjectValidator();
 ValidationResult result = validator.Validate(entity);


I'm not sure what type of HTML element FundingTypeId is but I'm assuming it is a dropdown list. If nothing is selected then it will give you this error. This is unfortunately one of the limitations of the FV integration with MVC which is caused by the bad design of MVC's default model binder. That message is not generated by FV but rather is produced by the DefaultModelBinder, in this case where the incoming value cannot be converted to the property type.

Check out these 2 questions which I posted on the Fluent Validation discussion forum: http://fluentvalidation.codeplex.com/discussions/250798 http://fluentvalidation.codeplex.com/discussions/253389

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜