Thymeleaf: th: value is ignored when using the th field:

I have a form where I want to edit some user data. Thus, already saved data is placed as th: value and after submitting I check with spring validation and want to return the form on the wrong input. I want the input field to have a user input value, but it always gives me the saved input.

What the input field looks like

<input type="text" th:value="${product.name}" th:field="*{name}" th:errorclass="fieldError"/> 

If the form is loaded the first time you enter the input fields, the values ​​of already saved data.

If it is loaded after sending and with a validation error, the input fields must have a user input value.

Is there any way to do this?

Thanks!

+6
source share
1 answer

Th attribute : the field will replace the value , id, and name attributes in your input .

Instead, use simple th: id , th: value and th: name without using th: field . Then you get what you want.

Then it will look like this:

 <input type="text" th:value="${product.name}" th:name="name" th:id="name" th:errorclass="fieldError"/> 

A similar answer here: How to set thymeleaf th value: field value from another variable

+13
source

Source: https://habr.com/ru/post/980755/


All Articles