子属性的选择性验证 - 在MVC流利的验证 [英] Selective validation of child properties - Fluent Validation in MVC

查看:105
本文介绍了子属性的选择性验证 - 在MVC流利的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用流利的验证与Ninject.Web.Mvc.FluentValidation库自动线了我所有的验证程序(和使用依赖注入创造了验证)。

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).

我创建了以下型号:

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; }
}

通过以​​下验证:

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();
    }
}

我的看法:

@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)

我试图做到的是要有一个有两个孩子的属性父模式。 Child1的属性是必需的,但CHILD2的属性是可选的。这工作正常,在正常情况下,但是当我使用Ninject模块自动线了验证器,那么它检测到没有为儿童类和接线所有的家长儿童属性的验证类型。

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.

有什么办法,我可以从没有摆脱Ninject模块的发生prevent呢?

Is there any way I can prevent this from happening without getting rid of the Ninject module?

推荐答案

由于自动wireup不会有一种方法来有条件地了解何时在模型绑定应用ChildValidator类,好像你有几个选择:

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:


  1. 决定是否子视图模型的重用是那么重要。面对这种情况,我可能会崩溃孩子到父的这种观点,如果孩子对象是不是很复杂,有不超过两视图,它使用的儿童单独对象的更多。我总是更有点舍不得是超级-DRY与视图模型,因为页面结构往往分歧随着时间的推移在我的经验。

  2. 清除的ModelState错误的CHILD2。 从这里,你可以采取CHILD2验证的完全控制,包括在这个独特的上下文CHILD2独立验证干脆手动应用它。这是我喜欢FluentValidation的原因之一 - 不同的验证逻辑适用于不同的上下文中相同的视图模型,不像数据注解的能力

  1. Decide if reuse of the child view models is that important. Faced with this situation, I would probably collapse the children into the parent for this view if the Child objects weren't very complex and there weren't more than a couple of views that used Child objects separately. I'm always a bit more reluctant to be super-DRY with view models, since page structures tend to diverge over time in my experience.
  2. Clear ModelState errors for Child2. From here, you could take complete control of validation for Child2, including a separate validator for Child2 altogether in this unique context and applying it manually. Which is one of the reasons I love FluentValidation - the ability to apply different validation logic to the same view model in different contexts, unlike data annotations.

自动wireup的值(即所有额外的code将其precludes)将排除转向了关闭这一情况,国际海事组织的选项。

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.

这篇关于子属性的选择性验证 - 在MVC流利的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆