1

After adding a Model attribute in the @GetMapping do you need add all fields using th:field="*{...}" in the view's form?

For example, if I add a User to the model and I want a form that edits the name but not the DOB or anything else can I just use th:field on just the name and not include the other fields?

It still seems to work when I create a @GetMapping for edit I pass the model and just have the th:field for just the name, but online I see people adding all of the fields to the form.

I can understand if you are trying to save the Entity, but if you're editing does it make a difference that I just don't understand?

andrewJames
  • 19,570
  • 8
  • 19
  • 51
NoobSailboat
  • 109
  • 9

1 Answers1

2

No, you don't need to use the th:field="*{...}" attribute on all fields. Any fields not specified will be reset to their empty or default value after the form is submitted.

This does mean that if you are editing an object and you leave a field off and save the object later, any fields without a th:field will be saved with an empty value. (You can use the @SessionAttributes annotation to persist fields even if they don't appear on the form.)

Metroids
  • 18,999
  • 4
  • 41
  • 52