6

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>();
RJP
  • 4,016
  • 5
  • 29
  • 42
  • Could you post the code where you perform the validate operation to see how you are returning the errors to the view ? – Javier Aug 26 '16 at 18:13
  • When you have an empty DoB, do you get the message "Please enter a valid date of birth", or another one ? I would guess you don't even go through validation by FluentValidation in this case (cause there's also validation for correct data type in mvc : and you try to pass a null value into a non nullable DateTime. Same if you write "a" into a textbox related to an int type) – Raphaël Althaus Aug 29 '16 at 15:00
  • Raphaël Althaus, you are correct! Making the DoB nullable fixed my issue! If you could post an actual response(and preferably with some details for the order of validation) the bounty is yours :) – RJP Aug 31 '16 at 05:09

2 Answers2

1

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.

Sunil Kumar
  • 3,142
  • 1
  • 19
  • 33
0

check validation issue at https://github.com/JeremySkinner/FluentValidation/issues/130

for details check this

https://github.com/JeremySkinner/FluentValidation/issues/127

bijayk
  • 556
  • 3
  • 18