public class ParentClass
{
public int ParentId { get; set; }
public int ActivityId { get; set; }
public DateTime UpateDate { get; set; }
public IList<ChildClass> ChildList { get; set; }
}
public class ParentClassValidator : AbstractValidator<ParentClass>
{
public ParentClassValidator()
{
RuleFor(x => x.ParentId).NotEmpty().WithMessage("Parent Id cannot be empty");
When(x => x.ParentId == 1,() =>
{
RuleFor(x => x.ActivityId).NotEmpty().WithMessage("Activity To cannot be empty.");
RuleFor(x => x.ChildList).SetCollectionValidator(new ChildClassValidator().Validate(new ChildClass(), "ParentId1"));
});
}
}
public class ChildClass
{
public int ChildId { get; set; }
public DateTime DueDate { get; set; }
}
public class ChildClassValidator : AbstractValidator<ChildClass>
{
public ChildClassValidator()
{
RuleFor(x => x.ChildId).NotEmpty().WithMessage("Child Id cannot be empty");
RuleSet("ParentId1",() =>
{
RuleFor(x => x.DueDate).Must(date => date != default(DateTime)).WithMessage("Due date cannot be empty");
});
}
}
親モデルのプロパティ値に基づいて子のリストを検証しようとしています。WebAPI - FluentValidation - 親モデルの値に基づいて子モデルのプロパティを検証する
私は、共通ルールと、親からのすべての子の特定のルールセットを検証する必要があります。私は以下のコードRuleFor(x => x.ChildList).SetCollectionValidator(new ChildClassValidator().Validate(new ChildClass(), "ParentId1"));
を親バリデーターで試しましたが、許可されていません
私はChild Model Validation using Parent Model Values. Fluent Validation. MVC4を経由しましたが、これは1つのプロパティのみです。