'FluentValidation, validate a property based on another properties value
This is my model:
public class StaffMemberTask
{
public int StaffMemberTaskId { get; set; }
public string TaskDescription { get; set; }
public Initiator InitiateFrom { get; set; }
public DateTime? StartFromDate { get; set; }
}
public enum Initiator
{
StartDate,
DateOfBirth,
SpecificDate,
Birthday
}
I want to validate that StartFromDate is not null (or maybe DateTime.Min) if InitiateFrom == Initiator.SpecificDate.
I have tried these combinations but I just cannot get the validation to trigger on StartFromDate:
RuleFor(x => x.StartFromDate).NotNull().DependentRules(() =>
{
RuleFor(x => x.InitiateFrom).Equals(Initiator.SpecificDate);
});
RuleFor(x => x.StartFromDate).NotNull().When(x => x.InitiateFrom == Initiator.SpecificDate);
RuleFor(p => p.StartFromDate).Cascade(CascadeMode.Stop).NotNull().When(x => x.InitiateFrom == Initiator.SpecificDate);
Does the fact I'm evaluating against an enum in anyway affect what I'm doing because I cannot understand why one of the above validation rules wouldn't have worked.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
