FluentValidation for When & must?

会有一股神秘感。 提交于 2019-12-05 02:14:20

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

You just need to change the order of your calls. Try this:

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

The When applies to all previous rules. So in your code when you applied it straight after the NotEmpty, it applied only to the NotEmpty rule and not to the Must rule.

Full demo on DotNetFiddle.

Tested in VERSION >= 8.4.0 a new enum parameter is added to When extension method

public enum ApplyConditionTo
{
    //
    // Summary: (default)
    //     Applies the condition to all validators declared so far in the chain.
    AllValidators = 0,
    //
    // Summary:
    //     Applies the condition to the current validator only.
    CurrentValidator = 1
}

by default (AllValidators): When will ending by rules chain ends

(CurrentValidator): When will be restricted to the first previous rule

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!