How to set thymeleaf th:field value from other variable

166,381

Solution 1

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

Solution 2

The correct approach is to use preprocessing

For example

th:field="*{__${myVar}__}"

Solution 3

If you don't have to come back on the page with keeping form's value, you can do that :

<form method="post" th:action="@{''}" th:object="${form}">
    <input class="form-control"
           type="text"
           th:field="${client.name}"/>

It's some kind of magic :

  • it will set the value = client.name
  • it will send back the value in the form, as 'name' field. So you would have to change your form field, 'clientName' to 'name'

If you matter keeping you form's input values, like a back on the page with an user input mistake, then you will have to do that :

<form method="post" th:action="@{''}" th:object="${form}">
    <input class="form-control"
           type="text"
           th:name="name"
           th:value="${form.name != null} ? ${form.name} : ${client.name}"/>

That means :

  • The form field name is 'name'
  • The value is taken from the form if it exists, else from the client bean. Which matches the first arrival on the page with initial value, then the forms input values if there is an error.

Without having to map your client bean to your form bean. And it works because once you submitted the form, the value arn't null but "" (empty)

Share:
166,381

Related videos on Youtube

Tobou
Author by

Tobou

Updated on April 05, 2020

Comments

  • Tobou
    Tobou about 4 years

    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}"
    
    • Avery
      Avery almost 10 years
      Define "is not working". What exactly is happening and what should be happening?
    • geoand
      geoand almost 10 years
      Why not populate the model with default value - thus moving logic away from the template and into the controller where it is probably better suited?
    • ziuu
      ziuu almost 9 years
      I think i answered simillar problem here stackoverflow.com/questions/25808433/…
  • Justin Smith
    Justin Smith almost 7 years
    Pointing out that this will ignore the bean binding.
  • Antoniossss
    Antoniossss over 6 years
    So in general this is how-not-to-do things.
  • want2learn
    want2learn over 6 years
    @JustinSmith Can you elaborate why this solution is not a correct way.
  • Justin Smith
    Justin Smith over 6 years
    This was a while ago, so it may no longer apply or be relevant, but to elaborate, th:value will set the value attribute of the input element. It does not bind the form to the bean, so if you change that field, then submit, the form data will ignore the binding. I wound up using the th:field attribute, and setting a default value from the controller.
  • Tamb
    Tamb about 6 years
    This is incorrect, it totally voids the effect of binding values
  • RiZKiT
    RiZKiT about 2 years
    For more informations see thymeleaf.org/doc/tutorials/3.0/…