Fluent验证第一次不验证整个表格

时间:2016-08-24 03:27:41

标签: 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>
}

我的流利验证码:

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");


        }

我的模特:

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; }
}

使用Autofac注册验证器:

builder.RegisterType<CustomerValidator>()
                .Keyed<IValidator>(typeof(IValidator<CustomerModel>))
                .As<IValidator>();

2 个答案:

答案 0 :(得分: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");
    }
}

希望它对你有所帮助。

答案 1 :(得分:0)