Fluent Validation does not check the entire form for the first time

So, I am using Fluent Validation in the form. When I click the "Submit" button and enter nothing, I get an error checking the date of birth. If I enter DoB, I get a confirmation for the name.

Why is this happening? I can’t understand what I did wrong.

My form:

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

My free verification code:

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

My model:

 public class CustomerModel { public CustomerModel() { } public Guid Id { get; set; } [DisplayName("First Name")] public string FirstName { get; set; } [DisplayName("DOB")] public DateTime DateOfBirth { get; set; } } 

Autofac authentication registration:

 builder.RegisterType<CustomerValidator>() .Keyed<IValidator>(typeof(IValidator<CustomerModel>)) .As<IValidator>(); 
+6
source share
2 answers

I also use Fluent Validation in my project in my project, it works the same way as you do. I tried my code in the same way as my code, it works fine, please see the code below:

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

Hope this helps you.

+1
source

All Articles