Selective validation of child properties - Fluent Validation in MVC

后端 未结 3 1869
天命终不由人
天命终不由人 2021-02-10 14:00

I\'m using Fluent Validation with the Ninject.Web.Mvc.FluentValidation library to automatically wire up all of my validators (and use dependency injection to create the validato

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-10 14:26

    You forget to set validator to second child property:

    public class ParentValidator : AbstractValidator
    {
        public ParentValidator()
        {
             RuleFor(model => model.Name).NotEmpty();
             RuleFor(model => model.Child1).SetValidator(new ChildValidator());
             RuleFor(model => model.Child2).SetValidator(new ChildValidator());
        }
    }
    

提交回复
热议问题