Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FluentValidation for When & must?

I am trying use FluentValidation validaton when dropdownlist value is yes and the field must be date. it is working when dropdownlist is yes checking for date. But also showing validation when I select No still it says Must be date.

It should not validate anymore if dropdownlist value otherthan the yes. How can we do that?

 RuleFor(x => x.DtPublishedTimeText)
            .NotEmpty()
            .When(HasMaterialPublishedElseWhereText)
            .WithMessage("Required Field")
            .Must(BeAValidDate)
            .WithMessage("Must be date");

private bool BeAValidDate(string val)
{
    DateTime date;
    return  DateTime.TryParse(val, out date);
}

private bool HasMaterialPublishedElseWhereText(MeetingAbstract model)
{
    return model.HasMaterialPublishedElseWhereText != null && 
             model.HasMaterialPublishedElseWhereText.Equals("yes");
}
like image 625
James123 Avatar asked Jun 03 '14 21:06

James123


People also ask

Should I use fluent validation?

Summary. 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.

What is FluentValidation .NET core?

Fluent Validation is a free to use . NET validation library that helps you make your validations clean, easy to create, and maintain. It even works on external models that you don't have access to, with ease. With this library, you can separate the model classes from the validation logic like it is supposed to be.

How do you write a test case for fluent validation?

Using the Code The program does the following basic steps: Creating a new Item object and setting the properties on that object. Creating an instance of the ItemValidator class to validate the properties on the object instance. Validating the values on the instance of the Item object.

What is fluent validation C#?

FluentValidation is a .NET library for building strongly-typed validation rules. It Uses a fluent interface and lambda expressions for building validation rules. It helps clean up your domain code and make it more cohesive, as well as giving you a single place to look for validation logic.


1 Answers

The issue you are having is the When predicate only applies to one rule. You need to have conditional validation on both the NotEmpty AND the Must.

There two ways to achieve this. Option 1 is tidier when there are only a couple of conditional rules, otherwise I'd use option 2.

RuleFor(x => x.DtPublishedTimeText)
    .NotEmpty()
        .When(HasMaterialPublishedElseWhereText)
        .WithMessage("Required Field")
    .Must(BeAValidDate)
        .When(HasMaterialPublishedElseWhereText)
        .WithMessage("Must be date");

Or

When(HasMaterialPublishedElseWhereText, () => {
    RuleFor(x => x.DtPublishedTimeText)
        .NotEmpty()
            .WithMessage("Required Field");
    RuleFor(x => x.DtPublishedTimeText)
        .Must(BeAValidDate)
            .WithMessage("Must be date");
});

Do note: I have no idea what HasMaterialPublishedElseWhereText is or what it looks like. I am assuming you can use it as a predicate


EDIT:

I'd also look at refactoring the HasMaterialPublishedElseWhereText method, the following is less error prone.

private bool HasMaterialPublishedElseWhereText(MeetingAbstract model)
{
    return String.Equals(model.HasMaterialPublishedElseWhereText, "yes", StringComparison.InvariantCultureIgnoreCase);
}
like image 67
Xenolightning Avatar answered Oct 09 '22 01:10

Xenolightning