[Validator(typeof(ProductDetailsRequestDTO))]
public class ProductDetailsRequestDTO
{
public int ArticleGroup { get; set; }
public DateTime ProducedAt { get; set; }
}
public class ProductDetailsRequestDTOValidator : AbstractValidator<ProductDetailsRequestDTO>
{
public ProductDetailsRequestDTOValidator()
{
RuleFor(r => r.ArticleGroup).NotEmpty().WithMessage("custom message");
RuleFor(r => r.ProducedAt).NotEmpty().WithMessage("custom message");
}
}
// FluentValidation setup
config.Services.Add(typeof(System.Web.Http.Validation.ModelValidatorProvider), new FluentValidationModelValidatorProvider());
fluentvalidation从哪里知道我创建的验证器,为什么我的模型总是正确的?
答案 0 :(得分:0)
这只是一个错字。您的模型使用错误的类型名称进行修饰。而不是像这样装饰它:
[Validator(typeof(ProductDetailsRequestDTO))]
public class ProductDetailsRequestDTO
{
public int ArticleGroup { get; set; }
public DateTime ProducedAt { get; set; }
}
使用验证器的类型装饰它:
[Validator(typeof(ProductDetailsRequestDTOValidator))]
public class ProductDetailsRequestDTO
{
public int ArticleGroup { get; set; }
public DateTime ProducedAt { get; set; }
}