流利验证不会在第一时间验证整个表单 [英] Fluent Validation doesn't validate the entire form the first time

查看:46
本文介绍了流利验证不会在第一时间验证整个表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I'm using Fluent Validation on a form. When I click submit and have nothing entered, I get a validation error for Date of Birth. If I enter a DoB, then I get the validation for First Name.

为什么会这样?我无法弄清楚接线错误.

Why is this happening? I can't figure out what I wired up wrong.

我的表单:

 @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中注册验证器:

Registering the validator with Autofac:

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

推荐答案

我也在我的项目中的项目中使用了Fluent Validation,它的工作方式与您所需的相同.运行正常,请参考以下代码:

I am also usingFluent Validation in my project in my project it is working the same way as you are required.I have tried your code same as my code it is working fine please refer below code:

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

希望它对您有帮助.

这篇关于流利验证不会在第一时间验证整个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆