【问题标题】:FluentValidation With Lists of ObjectsFluentValidation 与对象列表
【发布时间】:2012-05-31 15:54:01
【问题描述】:

现在我有一个包含三个输入列表的视图模型;文本框输入、下拉列表输入和复选框输入。这些列表中的每一个都是输入对象的列表,其中包含四个值;参数、参数名称、参数类型和值。我正在使用这些输入列表在表单上生成可变数量的字段,具体取决于每个列表包含的对象数量。

我目前的问题是我不确定如何使用流利的验证来验证列表对象中的变量。我知道每个列表的行为在返回 Nothing 时应该如何表现,但我不知道如何使用 FluentValidation 对该行为进行编码。

输入模型:

Public Class Input
    Property value As String
    Property ParamName As String
    Property ParamType As String
    Property ParamEnums As List(Of String)
End Class

参数视图模型:

Imports FluentValidation
Imports FluentValidation.Attributes

<Validator(GetType(ParamViewModelValidator))> _
Public Class ParamViewModel
    Property TextBoxInput As List(Of Input)
    Property DropdownListInput As List(Of Input)
    Property CheckBoxInput As List(Of Input)
End Class

我的观点:

@Modeltype SensibleScriptRunner.ParamViewModel

<h2>Assign Values to Each Parameter</h2>

@Code
    Using (Html.BeginForm("Index", "Parameter", FormMethod.Post))
    @<div> 
        <fieldset>
            <legend>Parameter List</legend>
            @For i = 0 To (Model.TextBoxInput.Count - 1)
                    Dim iterator = i
                    @Html.EditorFor(Function(x) x.TextBoxInput(iterator), "TextInput")
            Next
            @For i = 0 To Model.DropdownListInput.Count - 1
                    Dim iterator = i
                    @Html.EditorFor(Function(x) x.DropdownListInput(iterator), "EnumInput")
            Next
            @For i = 0 To Model.CheckBoxInput.Count - 1
                    Dim iterator = i
                    @Html.EditorFor(Function(x) x.CheckBoxInput(iterator), "CheckBoxInput")
            Next
            <p>
                <input type="submit" value="Query Server"/>
            </p>
        </fieldset>
    </div>
    Html.EndForm()
    End Using
End Code

编辑器模板之一的示例:

@modeltype SensibleScriptRunner.Input

@Code
    @<div class="editor-label">
        @Html.LabelFor(Function(v) v.value, Model.ParamName)
    </div>
    @<div class="editor-field">
        @Html.TextBoxFor(Function(v) v.value)
    </div>
End Code

当前 FluentValidation 代码:

Imports FluentValidation

Public Class ParamViewModelValidator
    Inherits AbstractValidator(Of ParamViewModel)

    Public Sub New()
        RuleFor(Function(x) x.TextBoxInput).NotEmpty.[When](Function(x) Not IsNothing(x.TextBoxInput))
        RuleFor(Function(x) x.DropdownListInput).NotEmpty.[When](Function(x) Not IsNothing(x.DropdownListInput))
        RuleFor(Function(x) x.CheckBoxInput).NotEmpty.[When](Function(x) Not IsNothing(x.CheckBoxInput))
    End Sub

End Class

我目前要做的事情是确认在我的每个列表中的每个对象中,它们都有一个不为空的值属性。我可以通过验证输入模型来做到这一点吗?现在,代码用于确认列表本身不为空,但列表中的对象仍然可以包含所有空值。有没有一种简单的方法可以做到这一点?

或者,我应该以不同的方式设置我的代码吗?

【问题讨论】:

    标签: vb.net asp.net-mvc-3 validation lambda fluentvalidation


    【解决方案1】:

    好吧,我想通了。对于任何好奇的人,我所要做的就是添加一个 inputvalidator 类。

    Imports FluentValidation
    
    Public Class InputValidator
    Inherits AbstractValidator(Of Input)
    
    Public Sub New()
        RuleFor(Function(x) x.value).NotEmpty()
        RuleFor(Function(x) x.ParamName).NotEmpty()
        RuleFor(Function(x) x.ParamType).NotEmpty()
    End Sub
    
    End Class
    

    将它添加到我的输入模型中解决了所有问题。我不确定代码在哪里实际检查此验证(我的编辑器模板指向输入作为模型,也许这与它有关;也可能是当我的 paramviewmodelvalidator 检查列表是否有效时,它还检查该列表中每个对象的标准(我认为是这个))。无论如何,如果有人遇到类似问题,这里有一个解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-21
      • 2021-10-21
      • 1970-01-01
      • 2012-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多