2017-08-10 6 views
0

FluentValidation の両方を同じViewModelで実行することはできますか?たとえば、私は次のViewModelを持っていた場合:FluentValidationとIValidatableObjectのエラーを組み合わせる

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"); 
    } 
} 

両方FooBar検証を実行し、私のコントローラがModelState.IsValidを呼び出したときに結果を返すことができる方法はありますか?

答えて

関連する問題