0

現在、入力の 3 つのリストを含むビューモデルがあります。textboxinput、dropdownlistinput、およびcheckboxinput。これらの各リストは、4 つの値を含む入力オブジェクトのリストです。paramenums、paramname、paramtype、および value。これらの入力リストを使用して、各リストに含まれるオブジェクトの数に応じてフォームに可変数のフィールドを生成しています。

私の現在の問題は、流暢な検証でリスト オブジェクト内の変数を検証する方法がわからないことです。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

ParamViewModel:

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

エディター テンプレートの 1 つの例:

@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

私が現在やりたいことは、各リスト内のすべてのオブジェクトで、それらすべてが何もない値属性を持っていることを確認することです。入力モデルを検証することでこれを行うことはできますか? 現在、コードはリスト自体が null ではないことを確認していますが、リスト内のオブジェクトにはすべて null 値が含まれている可能性があります。これを行う簡単な方法はありますか?

または、コードを別の方法で設定する必要がありますか?

4

1 に答える 1

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 がリストが有効かどうかをチェックするときに、そのリスト内の各オブジェクトの基準もチェックします(これだと思います))。とにかく、誰かが同様の問題を抱えている場合の解決策を次に示します。

于 2012-05-31T16:58:10.833 に答える