case class Address(
address1: String,
city: String,
state: String,
postal: String,
country: String
)
Form(
mapping = mapping(
"address1" -> nonEmptyText,
"city" -> nonEmptyText,
"state" -> nonEmptyText,
"postal" -> nonEmptyText,
"country" -> nonEmptyText
)(Address.apply)(Address.unapply).verifying("Invalid Postal Code!", validatePostal _)
)
def validatePostal(address: Address): Boolean = {
address.country match {
case "US" | "CA" =>
val regex: Regex = ("^(\\d{5}-\\d{4}|\\d{5}|\\d{9})$|^([a-zA-Z]\\d[a-zA-Z]( )?\\d[a-zA-Z]\\d)$").r
regex.pattern.matcher(address.postal).matches()
case _ => false
}
}
邮政编码的上述表单验证工作正常,表格中显示的全局错误表示无效的美国或加拿大邮政编码。
我想将错误显示为字段旁边的字段错误而不是全局错误,在我的情况下显示在表单顶部。
有没有办法使用内置的Form约束或验证方法来实现这一点,而不是FormError' s?
答案 0 :(得分:3)
您可以将约束添加到字段中。然后更新validatePostal以接受这两个值的元组。
Form(
mapping = mapping(
"address1" -> nonEmptyText,
"city" -> nonEmptyText,
"state" -> nonEmptyText,
"postal" -> tuple(
"code" -> nonEmptyText,
"country" -> nonEmptyText
).verifying("Invalid Postal Code!", validatePostal _),
)((address1, city, state, postal) => Address(address1, city, state, postal._1, postal._2))((address: Address) => Some((address.address1, address.city, address.state, (address.postal, address.country))))
)
模板:
@inputText(
addressForm("postal.code"),
'_label -> "Postal code",
'_help -> "Please enter a valid postal code.",
'_error -> addressForm.error("postal")
)
答案 1 :(得分:2)
定义错误就像在表单中创建FormError("","Invalid Postal Code!")
对象一样,因为它没有键(第一个参数),框架不会将错误附加到表单元素。
当您将请求绑定到表单时出现表单错误,您必须创建一个新表单,删除FormError("","Invalid Postal Code!")
并将其替换为错误FormError("form.id","message")
在我们的项目中,我们为Form创建了一个隐式def来替换表单错误(我们找不到创建动态约束验证的方法)这些是我们拥有的2个定义:
def replaceError(key: String, newError: FormError): Form[T] = {
val updatedFormErrors = form.errors.flatMap { fe =>
if (fe.key == key) {
if (form.error(newError.key).isDefined) None
else {
if (newError.args.isEmpty ) Some(FormError(newError.key,newError.message,fe.args))
else Some(newError)
}
} else {
Some(fe)
}
}
form.copy(errors = updatedFormErrors.foldLeft(Seq[FormError]()) { (z, fe) =>
if (z.groupBy(_.key).contains(fe.key)) z else z :+ fe
})
}
def replaceError(key: String, message: String, newError: FormError): Form[T] = {
def matchingError(e: FormError) = e.key == key && e.message == message
val oldError = form.errors.find(matchingError)
if (oldError.isDefined) {
val error = if (newError.args.isEmpty) FormError(newError.key,newError.message,oldError.get.args) else newError
form.copy(errors = form.errors.filterNot(e => e.key == key && e.message == message)).withError(error)
}
else form
}
我们在一个名为FormCryptBind
的类中有这些(因为我们还使用一些加密的东西改进了表单对象)并且我们定义了这样的隐式def:
implicit def formBinding[T](form: Form[T])(implicit request: Request[_]) = new FormCryptBind[T](form)
我们这样做是因为只是导入具有此隐式定义的对象,您可以使用所有FormCryptBind
定义,因为它们是Form的
我们就像这样使用它
import whatever.FormImprovements._
...
object SomeController extends Controller{
...
def submit = Action{ implicit request =>
form.bindRequest.fold(
formWithErrors => {
val newForm = formWithErrors.replaceError("", "formField.required", FormError("formField", "error.required")
BadRequest(someView(newForm)
},
formDetails => Redirect(anotherView(formDetails))
}
由于我无法从应用程序中输入实际的实时代码,所以我稍微触及了它:D因此如果你复制&糊