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) <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>();
source share