現在、入力の 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 値が含まれている可能性があります。これを行う簡単な方法はありますか?
または、コードを別の方法で設定する必要がありますか?