Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

thymeleaf: th:value is ignored when using th:field

Tags:

thymeleaf

I've got a form where I want to edit some user data. So the already stored data is put as th:value and after sending I validate with spring validation and want to give back the form on wrong input. I want the input field to have the value of the input by the user but it always gives me the stored input instead.

That's how an 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 the input fields should have the value of the already stored data.

If it's loaded after submit and with an validation error, the input fields should have the value of the user's input.

Is there a way to to that?

Thanks!

like image 701
Raphael Avatar asked Jan 09 '15 11:01

Raphael


2 Answers

Attribute th:field will replace attributes value, id and name in your input tag.

Instead, use plain th:id, th:value and th:name without using th:field. Then you will get what you wanted.

Then it will look like:

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

Similar answer is here: How to set thymeleaf th:field value from other variable

like image 96
Ostap Gonchar Avatar answered Nov 05 '22 01:11

Ostap Gonchar


Because Attribute th:field is Combination of both th:name and th:value So, either use this:

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

Or this:

<input type="text" th:field="*{name}" "th:errorclass="fieldError"/>
like image 34
Sriparna Ghosh Avatar answered Nov 05 '22 03:11

Sriparna Ghosh