html - Add binding error messages to custom messages in input tag -
i playing spring validating form input example - java spring mvc thymeleaf views. have been able pass messages view without issue. these display example wants them to...
e.g.
<td th:if="${#fields.haserrors('name')}" th:errors="*{name}">name error</td>
i trying find way put them html input validation attributes though have tried following (full page below)
<td><input type="text" th:field="*{age}" **required="required" data-errormessage-value-missing="${#fields.errors('age')}" data-errormessage="${#fields.errors('age')}"**/></td>
this has done no though , validation message displayed ${#fields.errors('age')} ! there way push binding errors attribute or misunderstanding way works ?
thanks in advance.
page html
<html>
<body> <form action="#" th:action="@{/}" th:object="${personform}" method="post"> <table> <tr> <td>name:</td> <td><input type="text" th:field="*{name}" required="required" data-errormessage-value-missing="custom message" /></td> <td th:if="${#fields.haserrors('name')}" th:errors="*{name}">name error</td> </tr> <tr> <td>age:</td> <td><input type="text" th:field="*{age}" required="required" data-errormessage-value-missing="${#fields.errors('age')}" data-errormessage="${#fields.errors('age')}"/></td> <td th:if="${#fields.haserrors('age')}" th:errors="*{age}">age error</td> </tr> <tr> <td><button type="submit">submit</button></td> </tr> </table> </form> </body> </html>
thymeleaf evaluates attributes start th:, in order this, have use th:attr. you're tag should like:
<input type="text" th:field="*{age}" required="required" th:attr="data-errormessage-value-missing=${#fields.errors('age')}, data-errormessage=${#fields.errors('age')}" />
you use plugin evaluate data tags, i've haven't used before can't comment on how works: https://github.com/mxab/thymeleaf-extras-data-attribute
Comments
Post a Comment