2011-10-09 101 views
26

有一个表格,用户可以输入事件的开始日期/时间和结束日期/时间。这里的验证至今: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(合并日期+时间属性),但不知道如何去做。

编辑: 为了澄清,我需要以某种方式合并结束日期+结束时间/起始日期+开始时间即DateTime.Parse(src.StartDate + “” + src.StartTime),然后验证EndDateTime对比的startDateTime - 怎么办我这样做?

回答

33

终于得到它的工作我重读documentation后:“请注意,是必须在附加重载,也接受的一个实例父对象被验证。“

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。

+0

是否有可能做同样的客户端? – SMC

+0

这在FluentValitation中不再有效。这是正确的答案:http://stackoverflow.com/a/20546097/59119 – Natrium

14

你可以可以尝试使用GreaterThan规则:

RuleFor(x => x.EndDate) 
    .GreaterThan(x => x.StartDate) 
    .WithMessage("end date must be after start date"); 
+0

对不起,刚刚在上面添加了一个说明。需要结合日期+时间和**然后**验证 – seekay

+0

是否有可能做同一个客户端?或请点击这里http://stackoverflow.com/questions/18142489/how-to-validate-date-in-clientside-using-fluentvalidation – SMC