Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.Linq;
- using System.Web;
- [AttributeUsage(AttributeTargets.Property)]
- public class DateGreaterThanAttribute : ValidationAttribute
- {
- public DateGreaterThanAttribute(string dateToCompareToFieldName)
- {
- DateToCompareToFieldName = dateToCompareToFieldName;
- }
- public string DateToCompareToFieldName { get; private set; }
- protected override ValidationResult IsValid(object value, ValidationContext validationContext)
- {
- if (value == null)
- {
- return ValidationResult.Success;
- }
- var laterDate = (DateTime?)value;
- var earlierDate = (DateTime?)validationContext
- .ObjectType
- .GetProperty(DateToCompareToFieldName)
- .GetValue(validationContext.ObjectInstance);
- if (laterDate > earlierDate)
- {
- return ValidationResult.Success;
- }
- return new ValidationResult("Date is not later than other.");
- }
- public override string FormatErrorMessage(string name)
- {
- return string.Format("{0} must be greather than {1}", name, this.DateToCompareToFieldName);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement