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
Since the auto-wireup wouldn't have a way to conditionally understand when to apply the ChildValidator class during model binding, it seems like you have a few alternatives:
The value of auto-wireup (i.e., all the extra code it precludes) would rule out the option of turning that off for this one case, IMO.
You forget to set validator to second child property:
public class ParentValidator : AbstractValidator<Parent>
{
public ParentValidator()
{
RuleFor(model => model.Name).NotEmpty();
RuleFor(model => model.Child1).SetValidator(new ChildValidator());
RuleFor(model => model.Child2).SetValidator(new ChildValidator());
}
}
If you don't want to auto wireup child validators you can add empty interface to child validator:
public class PersonalDataValidator : AbstractValidator, IChildValidator
And then in your factory:
public class FluentValidatorFactory : ValidatorFactoryBase
{
private readonly IKernel _kernel;
public FluentValidatorFactory(IKernel kernel)
{
_kernel = kernel;
}
public override IValidator CreateInstance(Type validatorType)
{
IValidator validator = _kernel.Resolve(validatorType) as IValidator;
////we dont want that windosr auto wires up all child validators.
var childValidator = validator as IChildValidator;
if (childValidator == null)
{
return validator;
}
return null;
}
}