지금 저는 세 가지 입력 목록을 포함하고있는 뷰 모델을 가지고 있습니다. textboxinput, dropdownlistinput 및 checkboxinput. 이러한 각 목록은 네 개의 값을 포함하는 입력 개체의 목록입니다. paramenums, paramname, paramtype 및 value가 있습니다. 이 입력 목록을 사용하여 각 목록에 포함 된 개체의 수에 따라 양식에서 가변 개수의 필드를 생성합니다.FluentValidation With Objects
현재 문제는 유창한 유효성 검사를 사용하여 목록 개체의 변수를 확인하는 방법을 모르겠다는 것입니다. 각 목록의 동작이 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.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
: 편집기 템플릿 중 하나의
@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
예3210
현재 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 값을 포함 할 수 있음을 확인하는 데 사용됩니다. 이 작업을 수행하는 간단한 방법이 있습니까?
다른 방법으로 코드를 설정해야합니까?