0

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?

Saedeas
  • 1,548
  • 8
  • 17

1 Answers1

1

Well, I figured it out. For anyone who is curious, all I had to do was add an inputvalidator class.

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

Adding this to my input model solved all the problems. I'm not sure where the code actually checks this validation (my editor templates point to input as the model, perhaps that has something to do with it; it could also be that when my paramviewmodelvalidator checks to see if a list is valid, it also checks the criteria of each object in that list (I'm thinking it's this one)). Regardless, here's a solution if anyone has a similar problem.

Saedeas
  • 1,548
  • 8
  • 17