asp.net-mvc-3 - FluentValidation-跨多个属性进行验证

标签 asp.net-mvc-3 fluentvalidation

具有用户可以输入事件的开始日期/时间和结束日期/时间的表单。到目前为止,这里是验证器:

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(合并的Date + Time属性)的验证,但是不确定如何执行。

编辑:
为了澄清,我需要以某种方式组合EndDate + EndTime / StartDate + StartTime,即DateTime.Parse(src.StartDate +“” + src.StartTime),然后验证EndDateTime与StartDateTime-我该怎么做?

最佳答案

在我重新阅读documentation之后,终于使它工作了:“请注意,Must还有一个额外的重载,它也接受正在验证的父对象的实例。”

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

可能有一种更清洁/更简洁的方法来执行此操作,但是到目前为止,它仍然有效。

关于asp.net-mvc-3 - FluentValidation-跨多个属性进行验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7701612/

相关文章:

c# - FluentValidation NotEmpty 消息未显示

c# - 注册带有条件的开放泛型

c# - 在 MediatR 管道上使用多个 FluentValidator

jquery - 无效的 JSON 类型 MVC 3

asp.net-mvc-3 - 是否可以保护 CloudConfigurationManager 引用的 Azure 连接字符串?

asp.net-mvc-3 - MVC3 - 将数据传递到模型之外的部分 View

c# - FluentValidation 和 ActionFilterAttribute - 在验证之前更新模型

c# - asp.net mvc 3 WebCache 奇怪的行为

asp.net - 如何将 Controller 上未经授权的 Ajax 请求重定向到登录页面? 。