Right now I have a viewmodel that contains three lists of inputs; textboxinput, dropdownlistinput, and checkboxinput. Each of these lists is a list of input objects, which contain four values; paramenums, paramname, paramtype, and value. I am using these input lists to generate a variable number of fields on a form dependent on how many objects each list contains.
My current problem is that I'm not sure how to validate the variables in the list objects with fluent validation. I know how the behavior of each list should behave with regards to returning Nothing, but I don't know how to code that behavior with FluentValidation.
Input Model:
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
My View:
@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
Example of one of the Editor templates:
@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
Current FluentValidation Code:
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
The thing I want to currently do is confirm that in every object in each of my lists, they all have a value attribute that isn't nothing. Can I do this by validating the input model? Right now, the code works at confirming that the list itself isn't null, but the objects in the list can still contain all null values. Is there a trivial way to do this?
Alternatively, should I have set my code up differently?