title: How to preserve attributes of nested objects on thymeleaf forms
body: <p>I've got an object which I want to load the form fields for dynamically.  Instantiating and presenting the fields is working appropriately on display, however, when I attempt to submit the form, all of the data is lost except the <code>${field.value}</code> which is provided by the user.  How does one preserve all of the additional attribute values?</p>
  
  <p>Here's the controller method that generates the form object:</p>
  
  <pre><code>@RequestMapping(method = RequestMethod.GET)
  public String newFooForm(@AuthenticationPrincipal UserDetails user, @RequestParam("fooType") String fooType, @RequestParam("isTeam") Boolean isTeam, Model model) {
      Employee currentUser = getCurrentUser(user);
      List&lt;FooFieldDefinition&gt; fooFieldDefinitions = fooFieldDefinitionService.findByOrganizationalUnitIdAndIsTeam(currentUser.getOrgId(), isTeam);
      FooViewModel fooViewModel = new FooViewModel(currentUser, fooType, isTeam, fooFieldDefinitions);
      model.addAttribute("fooViewModel", fooViewModel);
      return "newFooForm";
  }
  </code></pre>
  
  <p>The following snippet is called in the constructor <code>FooViewModel(currentUser, fooType, isTeam, fooFieldDefinitions)</code> which populates fooViewModel.fooFields:</p>
  
  <pre><code>fooFields = new ArrayList&lt;&gt;();
  for (FooFieldDefinition fooFieldDefinition : fooFieldDefinitions) {
      fooFields.add(new FooField(fooFieldDefinition));
  }
  </code></pre>
  
  <p>Here's the snippet of the form:</p>
  
  <pre><code>&lt;form id="foo-form" action="#" th:action="@{/fooForms}" th:object="${fooViewModel}"
        method="post"&gt;
      &lt;div th:each="fooField,iterationStatus  : *{fooFields}"&gt;
          &lt;label th:text="${fooField.label}"&gt;Label&lt;/label&gt;
          &lt;input th:type="text" th:field="*{fooFields[__${iterationStatus.index}__].value}"/&gt;
      &lt;/div&gt;
      &lt;div style="position: absolute; right: 20px; bottom: 20px;"&gt;
          &lt;input class="btn btn-primary" id="save" type="submit" name="save" value="Save"/&gt;
          &lt;input class="btn btn-primary" id="submit" type="submit" name="submit" value="Submit"/&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>I know that the fooField has the appropriate data associated with the instance when the form is generated for 2 reasons.  The <code>label</code> prints, and through debugging, I can see that the instances have the appropriate data before the view name is returned in the<code>newFooForm</code> method.</p>
  
  <p>Whenever I submit the form, all of the data for each <code>fooField</code> is set to null, except the value which is passed from the form.  I know this because, 1. The data is not preserved and 2. Debugging shows that.  Here's the controller method that's meant to hand the submission:</p>
  
  <pre><code>@RequestMapping(method = RequestMethod.POST, params = {"save"})
  public String fooFormDraftSubmit(@AuthenticationPrincipal UserDetails user, @ModelAttribute FooViewModel fooViewModel, Model model) {
      Employee currentUser = getCurrentUser(user);
      Foo foo = new Foo(currentUser, fooViewModel);
      fooService.save(foo);
      for (FooField fooField : fooViewModel.getFooFields()) {
         fooFieldService.save(fooField);
      }
      FooViewModel persistedFooViewModel = new FooViewModel(foo);
      model.addAttribute("fooViewModel", persistedFooViewModel);
      return "fooView";
  }
  </code></pre>
  
  <p>Thank you for any advice!</p>