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

后端 未结 3 990
一个人的身影
一个人的身影 2021-02-04 11:52

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

相关标签:
3条回答
  • 2021-02-04 12:26

    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

    0 讨论(0)
  • 2021-02-04 12:37

    Using th:field or value, id and name is ok. if you user th:field, you can write this:

    <input type="text" th:field="${product.name}" th:value="${product.name}" "th:errorclass="fieldError"/>
    
    0 讨论(0)
  • 2021-02-04 12:42

    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"/>
    
    0 讨论(0)
提交回复
热议问题