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.
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 Fluent Validation 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("D.O.B.")]
public DateTime DateOfBirth { get; set; }
}
Registering the validator with Autofac:
builder.RegisterType<CustomerValidator>()
.Keyed<IValidator>(typeof(IValidator<CustomerModel>))
.As<IValidator>();