2011-10-09 4 views
26

Avere un modulo in cui un utente può immettere la data/ora di inizio e la data/ora di fine di un evento. Ecco il validatore finora:FluentValidation: convalida su più proprietà

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

    } 

Ora vorrei anche aggiungere che la convalida data_ora_fine> (proprietà combinato Data + TIME) StartDateTime, ma non sapete come andare su di esso.

Edit: Per chiarire, ho bisogno di combinare qualche modo DataFine + EndTime/DataInizio + StartTime cioè DateTime.Parse (src.StartDate + "" + src.StartTime) e poi validare data_ora_fine vs. StartDateTime - come si fa Lo faccio?

risposta

33

Infine ottenuto lavorando dopo aver riletto la documentation: "Si noti che non v'è un sovraccarico aggiuntivo per i mosti che accetta anche un'istanza del oggetto principale che viene convalidato. "

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

Ci potrebbe essere un pulitore/modo più legant per fare questo, ma per ora, è worksforme.

+0

È possibile fare lo stesso lato client? – SMC

+0

Questo non è più valido in FluentValitation. Questa è la risposta corretta: http://stackoverflow.com/a/20546097/59119 – Natrium

14

Si potrebbe potrebbe provare a utilizzare il GreaterThan regola:

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

Siamo spiacenti, ho appena aggiunto un chiarimento sopra. È necessario combinare Data + Ora e ** quindi ** convalidare – seekay

+0

È possibile fare lo stesso lato client? o si prega di controllare qui http://stackoverflow.com/questions/18142489/how-to-validate-date-in-clientside-using-fluentvalidation – SMC