ScalaForms
ここにリンクされている例には、フォームの検証に関する次の例があります。
// You can also define ad-hoc constraints on the fields:
val loginForm = Form(
tuple(
"email" -> nonEmptyText,
"password" -> text
) verifying("Invalid user name or password", fields => fields match {
case (e, p) => User.authenticate(e,p).isDefined
})
)
バインディングエラーを介して、いくつかの制約が私のフォームに表示されます。(のようにnonEmptyText
、フィールドの後ろに余分な行が表示されますThis field is required
。参照:
loginForm.bindFromRequest.fold(
formWithErrors => // binding failure, you retrieve the form containing errors,
value => // binding success, you get the actual value
)
私がこれを行う.toString
と、制約formWithErrors
のためにこれを取得します:nonEmptyText
Form(ObjectMapping2(<function2>,<function1>,(Email adress,FieldMapping(,List(Constraint(Some(constraint.required),WrappedArray())))),(Password,FieldMapping(,List(Constraint(Some(constraint.required),WrappedArray())))),,List(Constraint(None,List()))),Map(Password -> test, Email adress -> ),List(FormError(Email adress,error.required,WrappedArray())),None)
後者の部分はFormErrorリストです。List(FormError(Email adress,error.required,WrappedArray())),None)
これはケースクラスです。case class FormError (key: String, message: String, args: Seq[Any])
ここで、key
は次のように定義されますThe error key (should be associated with a field using the same key).
。
FieldConstructorはこれを取得し、[メールアドレス]入力ボックスを赤くしてエラーメッセージを追加します([このフィールドは必須です])。
アドホック制約を表示しますか?
したがって、フォームフィールドがすべて入力されると、ユーザー名とパスワードがチェックされます。
verifying("Invalid user name or password", fields => fields match {
case (e, p) => User.authenticate(e,p).isDefined
})
しかし、「無効なユーザー名またはパスワード」をユーザーに表示する方法がわかりません。のリストは次のとおりです.toString
。FormError
formWithErrors
List(FormError(,Invalid user name or password,WrappedArray())),None)
Key
パーツは空です。
質問
アドホックエラーを表示するにはどうすればよいですか?
私も試しました:
BadRequest( views.html.test( formWithErrors ) ).flashing( "error" -> "Invalid user name or password." ) },
しかし、何らかの理由でフラッシュ経由でも機能しません:(。これ@flash.get("error").getOrElse("no 'error'")
により、毎回「エラーなし」になります。