2017-12-19 6 views
0

ちょうどこの素晴らしいapiを使用し始めた、私は複数のDependentRulesの問題に直面しています。それはすでにRequiredエラーが発生した、私はこの複数の従属ルールFluentValidation

RuleFor(d => d.NotificationType).NotEmpty().WithMessage("Required"); 
When(d => d.NotificationType.ToUpper() == "EMAIL",() => 
{ 
    RuleFor(d => d.NotificationEmail).EmailAddress().WithMessage("Invalid Email Address"); 
    RuleFor(d => d.NotificationEmail).NotEmpty().WithMessage("Required"); 

}); 
When(d => d.NotificationType.ToUpper() == "SMS",() => 
{ 
    RuleFor(d => d.NotificationContactNo).NotEmpty().WithMessage("Required"); 
}); 

のようなルールを持っていた。しかしNotificationTypeEmptyあるとき、これは失敗します。今の場合、これらの他のルールは従属ルールであり、NotificationTypeが空でない場合にのみ実行する必要があります。それが動作しているが、私は、このルールd.NotificationType).NotEmpty()を繰り返しています

RuleFor(d => d.NotificationType).NotEmpty().WithMessage("Required").DependentRules(k => 
    k.When(d => d.NotificationType.ToUpper() == "EMAIL",() => 
    { 
     RuleFor(d => d.NotificationEmail).EmailAddress().WithMessage("Invalid Email Address"); 
     RuleFor(d => d.NotificationEmail).NotEmpty().WithMessage("Required"); 
    }) 
); 
RuleFor(d => d.NotificationType).NotEmpty().WithMessage("Required").DependentRules(k => 
    When(d => d.NotificationType.ToUpper() == "SMS",() => 
    { 
     RuleFor(d => d.NotificationContactNo).NotEmpty().WithMessage("Required"); 
    }) 
); 

、私はこのような何か、Multiple Dependent Rules under one Ruleを達成したい。そのために私は、ルールを変更しました。

RuleFor(d => d.NotificationType).NotEmpty().WithMessage("Required").DependentRules(k => 
    k.When(d => d.NotificationType.ToUpper() == "EMAIL",() => 
    { 
     RuleFor(d => d.NotificationEmail).EmailAddress().WithMessage("Invalid Email Address"); 
     RuleFor(d => d.NotificationEmail).NotEmpty().WithMessage("Required"); 
    }); 
    k.When(d => d.NotificationType.ToUpper() == "SMS",() => 
    { 
     RuleFor(d => d.NotificationContactNo).NotEmpty().WithMessage("Required"); 
    }) 
); 

どのように私はこれを達成することができますか?

答えて

0

第1のルールでCascadeModeを設定して、最初の失敗で検証が停止するようにしてください。

RuleFor(d => d.NotificationType) 
    .Cascade(CascadeMode.StopOnFirstFailure) 
    .NotEmpty() 
    .WithMessage("Required"); 
+0

ような何かをすることによって私を助けました実行する必要のある他のルールが存在し、すべてのエラーをユーザーに示すように、この点の検証を行います。 –

0

それはとてもエレガントではありませんが、あなたの条件を変更することによって、失敗を回避することができます

RuleFor(d => d.NotificationType).NotEmpty().WithMessage("Required"); 

When(d => !string.IsNullOrEmpty(d.NotificationType) && d.NotificationType.ToUpper() == "EMAIL",() => 
{ 
    RuleFor(d => d.NotificationEmail).EmailAddress().WithMessage("Invalid Email Address"); 
    RuleFor(d => d.NotificationEmail).NotEmpty().WithMessage("Required"); 

}); 

When(d => !string.IsNullOrEmpty(d.NotificationType) && d.NotificationType.ToUpper() == "SMS",() => 
{ 
    RuleFor(d => d.NotificationContactNo).NotEmpty().WithMessage("Required"); 
}); 
+0

これは、私が行ったことです。あなたは '!string.IsNullOrEmpty'で' NotificationType'をチェックしていますが、 'RuleFor(d => d.NotificationType).NotEmpty()'のようにしています。それはすべて同じです –

0

あなたの解決策は、私は停止したくないこの

RuleFor(d => d.NotificationType).NotEmpty().WithMessage("Required") 
.DependentRules(k => 
    k.When(d => d.NotificationType.ToUpper() == "EMAIL",() => 
    { 
     RuleFor(d => d.NotificationEmail).EmailAddress().WithMessage("Invalid Email Address"); 
     RuleFor(d => d.NotificationEmail).NotEmpty().WithMessage("Required"); 
    }) 
).DependentRules(k => 
    k.When(d => d.NotificationType.ToUpper() == "SMS",() => 
    { 
     RuleFor(d => d.NotificationContactNo).NotEmpty().WithMessage("Required"); 
    }));