![](/img/trans.png)
[英]WebAPI - FluentValidation - Validate Child model properties based on parent model value
[英]How can I validate a property based on a value in a parent Model?
我有一个网站,人们可以在其中申请服务。 他们 select 是他们想要的产品和选项,以及有关他们自己的信息。
model分为几部分:
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; }
...
}
年龄取决于产品的类型,但我无法从ClientModel
访问ProductType
变量。
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;
}
}
如何访问ProductType
?
在 ApplicationInformationModel 中实现IValidatableObject
public class ApplicationInformationModel : IValidatableObject
{
public int ProductType { get; set; }
public ClientModel Client { get; set; }
...
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
//int productType = productType;
DateTime dob = Client.DateOfBirth;
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;
}
}
对ClientModel 的更改有一个 ApplicationInformationModel 。
public class ApplicationInformationModel
{
public int ProductType { get; set; }
...
}
public class ClientModel {
public string Name { get; set; }
[AgeValidation]
public DateTime DateOfBirth { get; set; }
public ApplicationInformationModel applicationInformationModel { get; set; }
...
}
然后创建一个继承自ValidationAttribute
的 class ,并覆盖IsValid
方法。
public class AgeValidationAttribute : ValidationAttribute
{
public string GetErrorMessage() => $"The age does not fall within the acceptable range.";
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
ClientModel client = (ClientModel)validationContext.ObjectInstance;
int productType = client.applicationInformationModel.productType;
DateTime dob = client.DateOfBirth;
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;
}
}
链接: 将自定义验证属性应用于数据字段
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.