Just started using this awesome api, I am facing some issue with multiple DependentRules
. I had rules like this
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");
});
But this fails when NotificationType
is Empty
,it already raised the Required
error. Now in this case these other rules are dependent rules and they should only execute when NotificationType
is not empty. For this i have modified the rules as:
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");
})
);
It is working but i am repeating this rule d.NotificationType).NotEmpty()
, I want to achieve something like this, 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");
})
);
Any idea how can i achieve this ?
You should set the CascadeMode on your first rule, so that the validation stops on the first failure.
RuleFor(d => d.NotificationType)
.Cascade(CascadeMode.StopOnFirstFailure)
.NotEmpty()
.WithMessage("Required");
It is not so elegant but you can avoid failure by modifying your conditions:
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");
});
Your solution helped me by doing something like this
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");
}));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With