Fluent Validation不会在第一次验证整个表单

RJP*_*RJP 6 asp.net-mvc fluentvalidation

所以我在表单上使用Fluent验证.当我点击提交并且没有输入任何内容时,我会收到出生日期的验证错误.如果我输入DoB,那么我将获得First Name的验证.

为什么会这样?我无法弄清楚我错了什么.

我的表格:

 @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        @Html.HiddenFor(customer => customer.CustomerIncomeInfo.CustomerEmploymentInfoModel.EmployerModel.Id)

            <!-- basic customer info -->
        <fieldset>
            <legend>Customer Info</legend>
            @Html.ValidationSummary(false, "Please correct the errors and try again", new { @class = "text-danger" })
            <div class="row">
                <div class="col-md-6">
                    <dl class="dl-horizontal">
                        <dt>@Html.LabelFor(model => model.FirstName)</dt>
                        <dd>@Html.EditorFor(model => model.FirstName, new {@class = "form-control"})</dd>

                    </dl>
                </div>
                <div class="col-md-6">
                    <dl class="dl-horizontal">
                        <dt>@Html.LabelFor(model => model.DateOfBirth)</dt>
                        <dd>@Html.EditorFor(model => model.DateOfBirth, new {@class = "form-control"})</dd>

                    </dl>
                </div>
            </div>
        </fieldset>
}
Run Code Online (Sandbox Code Playgroud)

我的流利验证码:

public CustomerValidator()
        {
            RuleFor(customer => customer.FirstName)
                .Length(3, 50)
                .NotEmpty()
                .WithMessage("Please enter a valid first name");

            RuleFor(customer => customer.DateOfBirth).NotEmpty().WithMessage("Please enter a valid date of birth");


        }
Run Code Online (Sandbox Code Playgroud)

我的型号:

public class CustomerModel
    {
        public CustomerModel()
        {
        }

        public Guid Id { get; set; }
        [DisplayName("First Name")]
        public string FirstName { get; set; }
        [DisplayName("D.O.B.")]
        public DateTime DateOfBirth { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

使用Autofac注册验证器:

builder.RegisterType<CustomerValidator>()
                .Keyed<IValidator>(typeof(IValidator<CustomerModel>))
                .As<IValidator>();
Run Code Online (Sandbox Code Playgroud)

Sun*_*mar 1

我也在Fluent Validation我的项目中使用它,它的工作方式与您需要的方式相同。我已经尝试过您的代码与我的代码相同,它工作正常,请参考下面的代码:

/// Test CODE 
/// Model Class
[Validator(typeof (CustomerModelValidator))]
public class CustomerModel {
    public CustomerModel() {}
    public Guid Id {
        get;
        set;
    }
    [DisplayName("First Name")]
    public string FirstName {
        get;
        set;
    }
    [DisplayName("D.O.B.")]
    public DateTime DateOfBirth {
        get;
        set;
    }
}
// Validator Class
public class CustomerModelValidator: AbstractValidator < CustomerModel > {
    public CustomerModelValidator() {
        RuleFor(customer = > customer.FirstName)
            .Length(3, 50)
            .NotEmpty()
            .WithMessage("Please enter a valid first name");
        RuleFor(customer = > customer.DateOfBirth).NotEmpty().WithMessage("Please enter a valid date of birth");
    }
}
Run Code Online (Sandbox Code Playgroud)

希望对您有帮助。