Search code examples
spring-mvcthymeleaf

preventing data to change in updateForm


  <form th:object="${owner}" class="form-horizontal" id="add-owner-form" method="post">
<div class="form-group has-feedback">
  <input
    th:replace="~{fragments/inputField :: input ('First Name', 'firstName', 'text')}" />
  <input
    th:replace="~{fragments/inputField :: input ('Last Name', 'lastName', 'text')}" />
  <input
    th:replace="~{fragments/inputField :: input ('Address', 'address', 'text')}" />
  <input
    th:replace="~{fragments/inputField :: input ('City', 'city', 'text')}" />
  <input
    th:replace="~{fragments/inputField :: input ('Telephone', 'telephone', 'text')}" />
    <input
          th:replace="~{fragments/inputField :: input ('Wallet', 'wallet', 'text')}" />
</div>
<div class="form-group">
  <div class="col-sm-offset-2 col-sm-10">
    <button
      th:with="text=${owner['new']} ? 'Add Owner' : 'Update Owner'"
      class="btn btn-default" type="submit" th:text="${text}">Add
      Owner</button>
  </div>
</div>

I wan't to allow user to change all data except wallet and I don't know how to do because if I don't include wallet value in form it simply disappears when submitted


Solution

  • One simple solution, and the one most people use, is to add the wallet as a hidden input, that way the user won't see it and you will still submit the right information.

    <input th:field="*{wallt}" hidden="hidden"/>
    

    Another way, is to receive the object, with the null values, and then fetch the original version and fill the remaining null fields from the original.

    Owner originalOwner = ownerService.fetchById(owner.getId());
    owner.setWallet(originalOwner.getWallet());