Razorblade
Razorblade

Reputation: 85

Thymeleaf, Spring nested backing object is not binding the values on form submit

I have a nested object and I'm using it as a model for a form.

public AgeBracketSet implements Serializable{
    private String id;
    private List<AgeBracket>  ageBrackets;

  /* Getters and Setters */
}

I have successfully bound all the properties of this object to the form and I can visualize their values when the view state is rendered. Here's a simplified version of how I'm doing it with Thymeleaf. Essentially, I loop through the items of the list and get their attributes.

<form id="bracketForm" role="form" th:action="${flowExecutionUrl}" th:object="${ageBracketSet}" method="post">
    <input th:id="'bracketSet_'+*{id}" th:field="*{id}" />
    <th:block th:each="bracket,loop : *{ageBrackets}" th:id="'bracket_'+${bracket.id}">
        <input th:id="'fromAge_'+${bracket.id}" th:field="*{ageBrackets[__${loop.index}__].fromAge}" />
        <input th:id="'toAge_'+${bracket.id}"  th:field="*{ageBrackets[__${loop.index}__].toAge}" />
    </th:block>
</form>

However, when I make changes in the form and submit it, the model remains unchanged. I have confirmed this by debugging the service that receives the form data. The model does not have the changes made in the form. Am I doing anything wrong here?

Upvotes: 1

Views: 2896

Answers (1)

Razorblade
Razorblade

Reputation: 85

I am embarrassed to say I've found the solution. The values simply weren't posted to the webflow for a lack of a 'name' attribute in each input. Using the same dynamically generated ID as a name did the job, and the bindings were correct for each item of the list. Like this:

<input th:id="'fromAge_'+${bracket.id}" th:name="'fromAge_'+${bracket.id}" th:field="*{ageBrackets[__${loop.index}__].fromAge}" />

Thanks to everyone who took the time to read this silly post. I'll be more careful next time ;)

Upvotes: 2

Related Questions