Forked from elizabeth-young/DateGreaterThanAttribute.cs
Created
September 25, 2017 01:54
-
-
Save LSTANCZYK/285cd9f5a2c8939744020e62dc691c19 to your computer and use it in GitHub Desktop.
Validation attribute for testing a data is later than another
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)] | |
public class DateGreaterThanAttribute : ValidationAttribute, IClientValidatable | |
{ | |
private const string DefaultErrorMessageFormatString = "The {0} field is required."; | |
public string OtherProperty { get; private set; } | |
public DateGreaterThanAttribute(string otherProperty) | |
{ | |
if (string.IsNullOrEmpty(otherProperty)) | |
{ | |
throw new ArgumentNullException("otherProperty"); | |
} | |
OtherProperty = otherProperty; | |
} | |
public override string FormatErrorMessage(string name) | |
{ | |
return string.Format(ErrorMessageString, name, OtherProperty); | |
} | |
protected override ValidationResult IsValid(object value, ValidationContext validationContext) | |
{ | |
//Get PropertyInfo Object && Get Value of the property | |
var basePropertyInfo = validationContext.ObjectType.GetProperty(OtherProperty); | |
var basePropertyValue = basePropertyInfo.GetValue(validationContext.ObjectInstance, null); | |
if(value == null || basePropertyValue == null) | |
{ | |
return null; | |
} | |
DateTime startDate = new DateTime(); | |
DateTime thisDate = new DateTime(); | |
try | |
{ | |
startDate = (DateTime)basePropertyValue; | |
thisDate = (DateTime)value; | |
} | |
catch(Exception e) | |
{ | |
var startSplit = basePropertyValue.ToString().Split('/'); | |
startDate = new DateTime(Convert.ToInt32(startSplit[2]), Convert.ToInt32(startSplit[1]), Convert.ToInt32(startSplit[0])); | |
var thisSplit = value.ToString().Split('/'); | |
thisDate = new DateTime(Convert.ToInt32(thisSplit[2]), Convert.ToInt32(thisSplit[1]), Convert.ToInt32(thisSplit[0])); | |
} | |
//Actual comparision | |
if (thisDate <= startDate) | |
{ | |
var message = FormatErrorMessage(validationContext.DisplayName); | |
return new ValidationResult(message); | |
} | |
//Default return - This means there were no validation error | |
return null; | |
} | |
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) | |
{ | |
var rule = new ModelClientValidationRule(); | |
rule.ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()); | |
//This string identifies which Javascript function to be executed to validate this | |
rule.ValidationType = "dategreaterthan"; | |
rule.ValidationParameters.Add("other", OtherProperty); | |
yield return rule; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment