public override bool IsValid(object value) { /*Type originalType = value.GetType();*/ /*var acadYear = value as originalType;*/ AcademicYearCreateSubmitModel academicYear = value as AcademicYearCreateSubmitModel; IAcademicYearService academicYearService = DependencyResolver.Current.GetService <IAcademicYearService>(); bool academicYearValid = academicYearService .AcademicYearExistsInDb(academicYear.StartDate, academicYear.EndDate) ? false : true; return(academicYearValid); }
public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { List <ValidationResult> errors = new List <ValidationResult>(); if (this.StartDate >= this.EndDate) { errors.Add(new ValidationResult("Start date should not be equal or greater than end date.")); } IAcademicYearService academicYearService = DependencyResolver.Current.GetService <IAcademicYearService>(); bool yearExistsInDb = academicYearService.AcademicYearExistsInDb(this.StartDate, this.EndDate) ? true : false; if (yearExistsInDb) { errors.Add(new ValidationResult( "Academic Year with the entered start year or end year already exists.")); } if (academicYearService.All().Any()) { AcademicYear latestAcademicYear = academicYearService.All().OrderByDescending(ay => ay.StartDate).First(); if (latestAcademicYear != null && this.StartDate.Year > latestAcademicYear.StartDate.Year + 1) { errors.Add(new ValidationResult( "New academic year cannot be more than 1 year later than latest academic year.")); } } if (this.EndDate.Year > this.StartDate.Year + 1) { errors.Add(new ValidationResult("Academic Year may not last more than 1 astronomical year.")); } return(errors); }