4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

play2.1 HTMLのテンプレートでvalidateエラーを表示する方法

Posted at

ヘルパーを使用せずにテンプレートを書く

というか書かざるをえないような場合があるとして、
フィールドコンストラクタを自作してもどうしようもないとか自作したくない場合に

input.scala.html
@(item:Form[Item])(implicit  flash:Flash,lang:Lang)

@main("新規追加"){
    <h2>〇〇入力画面的な</h2>
  <form method="post" action="@routes.Sample.save()">
  <div class="controls">
              <input type="text" id="id" name="id" value="@item.data.get("name")"  >
    </div>
     ・
     ・
     ・

という感じの入力ページのHTMLがあるとしてvalidat結果のエラーメッセージを表示するには

@Messages(item("name").errors.map(_.message).mkString(", "))

@if(item("name").hasErrors){@Messages(item("name").error.get.message)}
でエラーメッセージを出力できる
※上記はconf/messages.jaがある場合に日本語化している

コントローラでも同じような呼び出し方でエラーメッセージを取れるっぽいので
何かしらのハンドリングが必要な場合に使えるな

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?