4
votes

I have this assignment I am working on for school. Using SpringMVC, Hibernate JPA, and Thymeleaf. The following code below involves a specific attribute called " stringGrade ". I want to validate the input in that field using Hibernate Validator. I cannot seem to get Thymeleaf to read the expression. The arrayList that is looped in the view has a name attribute of " deliverables[0].stringGrade " and so on depending on how many there are. I have tried using " deliverables[${stat.index}].name " and this causes Thymeleaf to fail with this error:

HTTP Status 500 - Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "#fields.hasErrors('deliverables[0].stringGrade')" (menuItems/inputGrades:33)

All I want to do is have Thymeleaf be able to read the value using #fields.HasErrors and #fields.error. Below is the code that is relevant:

GradeCalculator Model:

public class GradeCalculator {

private ArrayList<Deliverable> deliverables;

Deliverable Model:

@Entity
@Table(name="Deliverables")
public class Deliverable implements Serializable {


@NotEmpty(message = "Required")
@Size(min = 1, max = 100, message = "Must be between 1 and 100")
@Digits(integer = 3, fraction = 0, message = "Must be a numeric value")
private String stringGrade; // String version of the grade ( Used for view input fields )

ThymeLeaf View:

<form th:object="${gradeCalculator}" action="#" th:action="@{/process/inputGrades}" method="POST" class="form-horizontal" role="form">

    <div th:each="deliverable,stat : ${grades.deliverables}">
        <div class="form-group">
            <p>Deliverable Name<span th:text="${grades.deliverables[__${stat.index}__].name}" name="name" id="name" class="badge tab-space"></span></p>
            <p>Deliverable Weight<span th:text="${grades.deliverables[__${stat.index}__].weight}" name="weight" id="weight" class="badge tab-space"></span></p>

            <h3><span class="label">Grade:</span></h3>

            <input type="text" th:field="${grades.deliverables[__${stat.index}__].stringGrade}" class="form-control" />
            <ul class="help-inline" th:if="${#fields.hasErrors('deliverables[__${stat.index}__].stringGrade')}">
                <li class="error" th:each="err : ${#fields.errors('deliverables[__${stat.index}__].stringGrade')}" th:text="${err}">Input is incorrect</li>
            </ul>
        </div>
    </div>

    <div class="form-group">
        <div class="text-center col-sm-10 col-sm-offset-2 col-md-4 col-md-offset-4">
                <button type="submit" class="btn btn-primary">Submit</button>
        </div>
    </div>

</form>
1

1 Answers

3
votes

Found the answer. I was not processing " deliverables[${stat.index}].stringGrade " correctly by using the Thymeleaf variable expression. I should have been doing this:

<ul class="help-inline" th:if="${#fields.hasErrors('${grades.deliverables[__${row.index}__].stringGrade}')}">
    <li
      class="error"
      th:each="err : ${#fields.errors('${grades.deliverables[__${row.index}__].stringGrade}')}" 
      th:text="${err}">
        Input is incorrect
    </li>
</ul>