liveBook · Manning

Th:field

As explained in Spring in Action, Sixth Edition

th:field is a Thymeleaf namespace attribute that binds a form element to a model property and manages related field attributes.

th:field is a Thymeleaf attribute that connects a form control to a property on the form’s model object. For checkbox collections, it supplies the field name and preserves checked state when the form is redisplayed after validation errors. section 2.1.3

Binding taco ingredients

A taco form can iterate through available wrap ingredients, assign each ingredient’s identifier with th:value, and bind every checkbox to the model’s ingredients property with th:field="*{ingredients}". Thymeleaf then renders controls such as name="ingredients" and value="FLTO". section 2.1.3

<h3>Designate your wrap:</h3>
<div th:each="ingredient : ${wrap}">
  <input th:field="*{ingredients}" type="checkbox" 
         th:value="${ingredient.id}"/>
  <span th:text="${ingredient.name}">INGREDIENT</span><br/>
</div>

Browse the complete checkbox pattern: iterate over model data, bind the ingredients collection, render each identifier, and display its label. section 2.1.3

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains attribute behavior and rendered HTML, but defers detailed validation handling.
Examples
Uses a taco-design form with iterated wrap ingredients, checkbox values, labels, and rendered HTML attributes.
What it assumes
Assumes a Thymeleaf form model and a collection of ingredients to iterate over.
What you can do after
Apply it via Thymeleaf to bind checkbox collections in a model-backed HTML form.
How it connects
Connects Thymeleaf, HTML forms, model binding, collections, and validation.

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.