是否可以在同一ViewModel中同时运行FluentValidation 和 IValidatableObject?例如,如果我有以下ViewModel:
public class TestViewModel
{
public Foo Foo { get; set; }
public Bar Bar { get; set; }
}
public class Foo : IValidatableObject
{
public string FooName { get; set; }
public IEnumerable<ValidationResult> Validate(System.ComponentModel.DataAnnotations.ValidationContext validationContext)
{
if (string.IsNullOrEmpty(FooName))
{
yield return new ValidationResult("Required from IValidatableObject", new[] { "FooName" });
}
}
}
[Validator(typeof(BarValidator))]
public class Bar
{
public string BarName { get; set; }
}
public class BarValidator : AbstractValidator<Bar>
{
public BarValidator() {
RuleFor(x => x.BarName).NotNull().WithMessage("Required from FluentValidation");
}
}
当我的控制器调用Foo
时,是否可以运行Bar
和ModelState.IsValid
验证并返回结果?
答案 0 :(得分:1)
答案是否定的,目前不是。请在此处查看回复:https://github.com/JeremySkinner/FluentValidation/issues/545#issuecomment-322757974