How to set thymeleaf th:field value from other variable

Tobou picture Tobou · Jul 30, 2014 · Viewed 120.5k times · Source

I have a simple text input field where i have to set default value from one object and save its final value in other. The following code is not working.

<div th:object="${form}">
    <input class="form-control"
           type="text"
           th:value="${client.name}"  //this line is ignored
           th:field="*{clientName}"/>
</div>

form is DTO object and client is Entity object from database.

What is the correct way to solve this situation?

By not working I mean - lets say that initial values are client.name="Foo" and form.clientName=null. I need that input field display value is "Foo" and after form submission form.clientName value to become "Foo". But the input field displays nothing and on submission form.clientName value still is null;

If anyone is interested, solved this issue using following structure (found the answer in another question).

th:attr="value = ${client.name}"

Answer

Newbie picture Newbie · Aug 22, 2014

You could approach this method.

Instead of using th:field use html id & name. Set value using th:value

<input class="form-control"
           type="text"
           th:value="${client.name}" id="clientName" name="clientName" />

Hope this will help you