Is there a way to create a custom attribute in ASP.NET Core to validate if one date property is less than other date property in a model using ValidationAttribute<
You could compare the two dates in the IsValid()
method.
public class CompareDates : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
// get your StartDate and EndDate from model and value
// perform comparison
if (StartDate < EndDate)
{
return new ValidationResult("start date must be less than the end date");
}
else
{
return ValidationResult.Success;
}
}
}