FluentValidation - validating across multiple properties

前端 未结 2 1749
伪装坚强ぢ
伪装坚强ぢ 2020-12-29 02:33

Have a form where a user can enter start date/time and end date/time for an event. Here\'s the validator so far:

public class EventModelValidator : AbstractV         


        
相关标签:
2条回答
  • 2020-12-29 03:33

    Finally got it working after I re-read the documentation: "Note that there is an additional overload for Must that also accepts an instance of the parent object being validated."

    public class EventModelValidator : AbstractValidator<EventViewModel>
        {
            public EventModelValidator()
            {
                RuleFor(x => x.StartDate)
                    .NotEmpty().WithMessage("Date is required!")
                    .Must(BeAValidDate).WithMessage("Invalid date");
                RuleFor(x => x.StartTime)
                    .NotEmpty().WithMessage("Start time is required!")
                    .Must(BeAValidTime).WithMessage("Invalid Start time");
                RuleFor(x => x.EndTime)
                    .NotEmpty().WithMessage("End time is required!")
                    .Must(BeAValidTime).WithMessage("Invalid End time")
                    // new
                    .Must(BeGreaterThan).WithMessage("End time needs to be greater than start time");
                RuleFor(x => x.Title).NotEmpty().WithMessage("A title is required!");
            }
    
    
            private bool BeAValidDate(string value)
            {
                DateTime date;
                return DateTime.TryParse(value, out date);
            }
    
            private bool BeAValidTime(string value)
            {
                DateTimeOffset offset;
                return DateTimeOffset.TryParse(value, out offset);
            }
            // new
            private bool BeGreaterThan(EventViewModel instance, string endTime)
            {
                DateTime start = DateTime.Parse(instance.StartDate + " " + instance.StartTime);
                DateTime end = DateTime.Parse(instance.EndDate + " " + instance.EndTime);
                return (DateTime.Compare(start, end) <= 0);
            }
        }
    

    There might be a cleaner/more legant way to do this, but for now, it worksforme.

    0 讨论(0)
  • 2020-12-29 03:37

    You could could try using the GreaterThan rule:

    RuleFor(x => x.EndDate)
        .GreaterThan(x => x.StartDate)
        .WithMessage("end date must be after start date");
    
    0 讨论(0)
提交回复
热议问题