FluentValidation - 验证跨越多个属性 [英] FluentValidation - validating across multiple properties

查看:1226
本文介绍了FluentValidation - 验证跨越多个属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个表格,其中用户可以输入开始日期/时间和结束日期/时间的事件。这里的验证至今:

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

    }

现在我也想补充验证了EndDateTime>的startDateTime(合并日期+时间属性),但不知道如何去做。

Now I'd also like to add validation that EndDateTime > StartDateTime (combined Date+Time properties), but not sure how to go about it.

编辑:
为了澄清,我需要以某种方式结合起来结束日期+结束时间/起始日期+开始时间,即DateTime.Parse(src.StartDate ++ src.StartTime),然后验证EndDateTime主场迎战的startDateTime - ?我该怎么办

To clarify, I need to somehow combine EndDate + EndTime/StartDate + StartTime i.e. DateTime.Parse(src.StartDate + " " + src.StartTime) and then validate EndDateTime vs. StartDateTime - how do I do that?

推荐答案

终于有工作,我重读的文档。请注意,是必须在还接受正在验证的父对象的实例,一个额外的过载

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

有可能做到这一点更清洁/更legant的方式,但现在,它WORKSFORME。

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

这篇关于FluentValidation - 验证跨越多个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆