2012-05-31 3 views
0

지금 저는 세 가지 입력 목록을 포함하고있는 뷰 모델을 가지고 있습니다. 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 값을 포함 할 수 있음을 확인하는 데 사용됩니다. 이 작업을 수행하는 간단한 방법이 있습니까?

다른 방법으로 코드를 설정해야합니까?

답변

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가 목록이 유효한지 검사 할 때도 가능하다. 또한 그 목록에있는 각 객체의 기준을 검사합니다 (나는 이것이이 것이라고 생각합니다)). 어쨌든, 비슷한 문제가있는 사람이라면 여기 해결책이 있습니다.