I've got a website where people can apply for services. They select a product and options they'd like, along with information about themselves.
The model is divided into parts:
public class ApplicationInformationModel
{
public int ProductType { get; set; }
public ClientModel Client { get; set; }
...
}
public class ClientModel {
public string Name { get; set; }
[AgeValidation]
public DateTime DateOfBirth { get; set; }
...
}
The age is dependent on the type of the product, but I don't have access to the ProductType
variable from the ClientModel
.
public class AgeValidationAttribute : ValidationAttribute
{
public string GetErrorMessage() => $"The age does not fall within the acceptable range.";
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
int productType = // How do I get the productType?
var dob = (DateTime)value;
int age = DateTime.Now.Year - dob.Year;
if (DateTime.Now.DayOfYear < dob.DayOfYear) age--;
if (productType == 0 && age < 18)
{
return new ValidationResult(GetErrorMessage(validationContext.DisplayName));
}
else if (productType == 1 && age < 21)
{
return new ValidationResult(GetErrorMessage(validationContext.DisplayName));
}
return ValidationResult.Success;
}
}
How do I get access to the ProductType
?