Asp.net mvc 3 FluentValidation.Net不';当使用.SetValidator()时,不会产生客户端不引人注目的验证

Asp.net mvc 3 FluentValidation.Net不';当使用.SetValidator()时,不会产生客户端不引人注目的验证,asp.net-mvc-3,fluentvalidation,Asp.net Mvc 3,Fluentvalidation,我正在尝试让客户端验证为使用编辑器模板的页面工作 我的视图模型的简化示例如下: [Validator(typeof(ValidationTestModelValidator))] public class ValidationTestModel { public string Name { get; set; } public string Age { get; set; } public ChildModel Child { get; set; } } publi

我正在尝试让客户端验证为使用编辑器模板的页面工作

我的视图模型的简化示例如下:

[Validator(typeof(ValidationTestModelValidator))]
public class ValidationTestModel
{
    public string Name { get; set; }

    public string Age { get; set; }

    public ChildModel Child { get; set; }
}
public class ChildModel
{
    public string ChildName { get; set; }

    public string ChildAge { get; set; }
}
子模型为,例如:

[Validator(typeof(ValidationTestModelValidator))]
public class ValidationTestModel
{
    public string Name { get; set; }

    public string Age { get; set; }

    public ChildModel Child { get; set; }
}
public class ChildModel
{
    public string ChildName { get; set; }

    public string ChildAge { get; set; }
}
我的验证器是,例如:

public class ValidationTestModelValidator : AbstractValidator<ValidationTestModel>
{
    public ValidationTestModelValidator()
    {
        RuleFor(m => m.Name)
            .NotEmpty()
            .WithMessage("Please enter the name");

        RuleFor(m => m.Age)
            .NotEmpty()
            .WithMessage("Please enter the age");

        RuleFor(m => m.Age)
            .Matches(@"\d*")
            .WithMessage("Must be a number");

        RuleFor(m => m.Child)
            .SetValidator(new ChildModelValidator());
    }
}
public class ChildModelValidator : AbstractValidator<ChildModel>
{
    public ChildModelValidator()
    {
        RuleFor(m => m.ChildName)
            .NotEmpty()
            .WithMessage("Please enter the name");

        RuleFor(m => m.ChildAge)
            .NotEmpty()
            .WithMessage("Please enter the age");

        RuleFor(m => m.ChildAge)
            .Matches(@"\d*")
            .WithMessage("Must be a number");
    }
}
这将为两个属性Name和Age生成不引人注目的客户端验证,但对ChildModel上的属性没有任何验证

知道我做错了什么吗


更新:如果我只是用Validator属性对ChildModel进行注释,似乎还可以,但是我希望有条件地应用验证,因此使用SetValidator()。

我尝试分析文档。它声明MVC集成在默认情况下基于属性工作,因为它的验证器工厂使用该属性来实例化适当的验证器。显然,它确实验证了主模型上属性引用的ChildModel,但这可能只是模型结构的自动递归遍历,以生成客户端验证代码。因此,它可能使用相同的机制为ChildModel类型定位适当的验证器。如果删除SetValidator规则(但保留ChildModel的属性),您是否可以测试会发生什么,它是否仍然会基于该属性生成验证器

客户端支持的验证器有一个明确的列表,但不幸的是没有提到(或解释)SetValidator,这是一个坏迹象

该文档还指出,客户端不支持使用条件的规则,因此很可能无法使用SetValidator解决此问题(正如您在更新中所述)

另请参阅有关客户端条件规则的讨论:。有人建议用jQuery验证在JS中实现条件规则,但是,如果有一个复杂的验证需要有条件地添加,这可能会很复杂


让它添加具有该属性的验证器,但之后以某种方式抑制它们,怎么样?例如,通过随后从元素中删除不引人注目的属性?

正如您在更新中指出的那样,如果您希望发出不引人注目的标记,则需要使用验证器修饰子类型。然后,您可以在“父”验证器类中有条件地使用它,方法是将其包装在if块中,或者使用FluentValidation的When()方法来使用表达式

此外,请注意,您还可以链接属性的规则(即使用fluent界面):


如果其他人也有同样的问题:

您还需要在
ChildModel
上添加验证器属性

[Validator(typeof(ChildModelValidator))]
public class ChildModel

并保持所有其他代码不变,然后它在客户端工作

RuleFor
这是来自mvc库还是外部库?如果你用的是哪一个?@DarthVader:这是从图书馆来的