Quantcast

th:field and th:errors

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

th:field and th:errors

alex916
There is a way to user a variable inside th:field and th:errors?
Something like:
<th:block th:with="attribute=username"
<input type="text" th:field="*{attribute}" />
<span th:if="${#fields.hasErrors(attribute)}" th:errors="*{attribute}" class="text-danger help-block" ></span>
</th:block>

Thanks
Alex
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: th:field and th:errors

danielfernandez
Administrator
Expressions contained in th:field and th:errors are almost-directly passed to the Spring MVC form binding infrastructure, and they don't expect variables there, but property names.

So if you need to use variable property names, I'd suggest using the expression preprocessing mechanism: http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#preprocessing
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: th:field and th:errors

alex916
Thank you Daniel, you are right!

In this way everything works:

<th:block th:with="attribute=username">
      <input type="text" th:field="*{__${attribute}__}" />
      <span th:if="${#fields.hasErrors(attribute)}" th:errors="*{__${attribute}__}" class="text-danger help-block" ></span>
</th:block>
Loading...