Spring validating form inputの例 - Thymeleafビューのjava spring mvcで再生しています。私は問題なしでビューにメッセージを渡すことができました。これらの例では、これらの例が表示されます。入力タグのカスタムメッセージにバインディングエラーメッセージを追加します。
私はこれはしかし何も良いが行われていないとしている<td><input type="text" th:field="*{age}" **required="required" data-errormessage-value-missing="${#fields.errors('age')}" data-errormessage="${#fields.errors('age')}"**/></td>
ので、私は、次の(以下フルページ)を試してみましたけれども、HTMLの入力検証属性にそれらを配置する方法を見つけようとしています
<td th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name Error</td>
表示される検証メッセージは$ {#fields.errors( 'age')}です!バインディングエラーを属性にプッシュする方法はありますか、それとも機能しているか誤解していますか?
ご協力いただきありがとうございます。
ページの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>
こんにちは、ありがとうございました。それは問題を解決していませんが、正しい方向に私を指摘しています。 th:attr属性は正しいように見えますが、私のコードはエラーメッセージを選択しません。 ifエラーが存在し、テキスト行を出力するので、バインディングエラーオブジェクトを照会することができる必要がありますが、まだ特定のメッセージを取得できませんでした。私は今停止して、あなたが質問の要点に答えたと信じていると信じています(このコメントは、後で誰かが私を答えとしてマークしたのですが実際のコード行はうまくいきません! – gringogordo