RJP
RJP

Reputation: 4116

Fluent Validation doesn't validate the entire form the first time

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

Upvotes: 6

Views: 1536

Answers (2)

Sunil Kumar
Sunil Kumar

Reputation: 3242

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

Hope it helps you.

Upvotes: 1

Related Questions