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 validators).
I created the following Models:
public class Parent
{
public string Name { get; set; }
public Child Child1 { get; set; }
public Child Child2 { get; set; }
}
public class Child
{
public string ChildProperty { get; set; }
}
With the following validators:
public class ParentValidator : AbstractValidator<Parent>
{
public ParentValidator()
{
RuleFor(model => model.Name).NotEmpty();
RuleFor(model => model.Child1).SetValidator(new ChildValidator());
}
}
public class ChildValidator : AbstractValidator<Child>
{
public ChildValidator()
{
RuleFor(model => model.ChildProperty).NotEmpty();
}
}
My Views:
@model Parent
@using(Html.BeginForm())
{
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
@Html.EditorFor(model => model.Child1)
@Html.EditorFor(model => model.Child2)
<input type="submit" value="Save" />
}
@model Child
@Html.EditorFor(model => model.ChildProperty)
@Html.EditorFor(model => model.ChildProperty)
What I am trying to accomplish is to have a parent model that has two child properties. Child1's property is required but Child2's property is optional. This works fine under normal circumstances, but when I use the Ninject module to wire up the validators automatically, then it is detecting that there is a validator type for the Child class and wiring up all of the Child properties on the Parent.
Is there any way I can prevent this from happening without getting rid of the Ninject module?