加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – Fluent Validation不会在第一次验证整个表单

发布时间:2020-12-16 03:46:02 所属栏目:asp.Net 来源:网络整理
导读:所以我在表单上使用Fluent验证.当我点击提交并且没有输入任何内容时,我会收到出生日期的验证错误.如果我输入DoB,那么我将获得First Name的验证. 为什么会这样?我无法弄清楚我错了什么. 我的表格: @using (Html.BeginForm()) { @Html.AntiForgeryToken() @H
所以我在表单上使用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>();

解决方法

我也在我的项目中使用了流程验证,它的工作方式与您的需求相同.我已经尝试过与我的代码相同的代码,它工作正常,请参考下面的代码:

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

希望它能帮到你.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读