public static bool ValidFlag;
[AttributeUsage(AttributeTargets.Property)]
public class OrderDateValidationAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
// Here you can perform your custom validation and return true/false value
// you will get the entered OrderDate value in “value” and validationContext will have other row cell data
...
return ValidFlag ? new ValidationResult("Custom Message", new[] { validationContext.MemberName }) : ValidationResult.Success;
}
}
public class Order
{
public int? OrderID { get; set; }
public string CustomerID { get; set; }
[OrderDateValidation]
public DateTime? OrderDate { get; set; }
public DateTime? ShippedDate { get; set; }
public string ShipCountry { get; set; }
}
|
Did I miss something?
[AttributeUsage(AttributeTargets.Property)]
public class OrderDateValidationAttribute : ValidationAttribute
{
public static bool ValidFlag = false;
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
string ErrorMessage = "";
...
if(returnDate < borrowDate)
{
ValidFlag = true;
ErrorMessage = "...";
}
else if(returnDate > DateTime.Now)
{
ValidFlag = true;
ErrorMessage = "...";
}
return ValidFlag ? new ValidationResult(ErrorMessage, new[] { validationContext.MemberName }) : ValidationResult.Success;
}
}
|