My goal is to populate html page with multiple similar forms from list of objects. each form with "submit to edit button". By submitting one of these forms I'd like to receive on controller the single object from that particular form
my form object looks like
@Getter
@Setter
@ToString
public class SimpleFormData {
private List<SyncedFieldDTO> syncedFields = new ArrayList<>();
private SyncedFieldDTO editedSyncedField = new SyncedFieldDTO();
}
SyncedFieldDTO
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class SyncedFieldDTO {
private Long id;
private JiraFieldDTO jiraField;
}
and nested JiraField object
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class JiraFieldDTO {
private Long id;
}
controller
@Controller
@Slf4j
@RequiredArgsConstructor
public class SimpleFormController {
private final SyncedFieldsRepository syncedFieldsRepository;
@PostMapping("/simpleedit")
public String editForm(SimpleFormData simpleFormData) {
log.info("receivedFormData {}", simpleFormData);
return "simpleForm";
}
@GetMapping("/simple")
public String showForm(SimpleFormData simpleFormData) {
log.info("syncedFields are - {}", syncedFieldsRepository.findAll());
List<SyncedFieldDTO> syncedFieldDTOList = syncedFieldsRepository
.findAll().stream()
.map(SyncedFieldDTO::new).toList();
simpleFormData.setSyncedFields(syncedFieldDTOList);
log.info("prepared syncedFieldsFormData - {}", simpleFormData);
return "simpleForm";
}
}
thymeleaf template
<body>
<div th:each="syncedField, iStat : ${simpleFormData.syncedFields}">
<form th:action="@{/simpleedit}" th:object="${simpleFormData.editedSyncedField}" method="post">
<pre th:text="'syncedField id = ' + ${syncedField.id}"></pre>
<pre th:text="'jiraField id = ' + ${syncedField.jiraField.id}"></pre>
<input th:value="${simpleFormData.syncedFields[__${iStat.index}__].id}"
th:name="id"
th:id="id"
>
<input th:value="${simpleFormData.syncedFields[__${iStat.index}__].jiraField.id}"
th:name="jiraField.id"
th:id="jiraField.id"
>
<input type="submit to edit" value="Edit"/> <input type="reset" value="Reset">
</form>
</div>
</body>
the goal is to receive not empty simpleFormDate.editedSyncedField attribute at editForm method For two days I've read similar questions, like
Thymeleaf form with multiple objects of the same class
Thymeleaf: Edit List of Objects for form
played with th:object th:form th:value th:name th:id read thymeleaf doc but failed to find the working solution
would appreciate any help