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:List(FormError(Email adress,error.required,WrappedArray())),None)
这是一个案例类:case class FormError (key: String, message: String, args: Seq[Any])
wherekey
被定义为: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 工作:(。这@flash.get("error").getOrElse("no 'error'")
每次都给我“没有错误”。