结合FluentValidation和IValidatableObject错误

时间:2017-08-10 17:54:17

标签: c# asp.net-mvc razor asp.net-core fluentvalidation

是否可以在同一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时,是否可以运行BarModelState.IsValid验证并返回结果?

1 个答案:

答案 0 :(得分:1)

答案是否定的,目前不是。请在此处查看回复:https://github.com/JeremySkinner/FluentValidation/issues/545#issuecomment-322757974