Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>();
like image 388
RJP Avatar asked Aug 24 '16 03:08

RJP


People also ask

Is fluent validation good?

FluentValidation provides a great alternative to Data Annotations in order to validate models. It gives better control of validation rules and makes validation rules easy to read, easy to test, and enable great separation of concerns.

How does fluent validation works?

FluentValidation is a server-side library and does not provide any client-side validation directly. However, it can provide metadata which can be applied to the generated HTML elements for use with a client-side framework such as jQuery Validate in the same way that ASP. NET's default validation attributes work.

How do you debug fluent validation rules?

There is no way to debug Fluent Validator code with Visual Studio tools. You need to comment the specific part of code (RuleFor) that you want to test. Keep doing it until all rules are tested.

Is fluent validation free?

FluentValidation is developed for free by @JeremySkinner in his spare time and financial sponsorship helps keep the project going. Please sponsor the project via either GitHub sponsors or OpenCollective.


1 Answers

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.

like image 147
Sunil Kumar Avatar answered Oct 13 '22 00:10

Sunil Kumar