Forms 播放2.3 scala表单-如何自定义约束消息

Forms 播放2.3 scala表单-如何自定义约束消息,forms,scala,playframework,constraints,customization,Forms,Scala,Playframework,Constraints,Customization,我已经在play框架中创建了带有约束的表单: val voucherForm = Form( mapping( "voucherName" -> nonEmptyText, "voucherCode" -> optional(text(minLength = 6).verifying(pattern("""[a-zA-Z0-9]+""".r, error = "..."))) )(VoucherForm.apply)(VoucherForm.unap

我已经在play框架中创建了带有约束的表单:

val voucherForm = Form(
  mapping(
    "voucherName" -> nonEmptyText,
    "voucherCode" -> optional(text(minLength = 6).verifying(pattern("""[a-zA-Z0-9]+""".r, error = "...")))     
  )(VoucherForm.apply)(VoucherForm.unapply)
)
当我在网页上显示此表单时,输入框附近会显示约束消息(如
必需
最小长度:6,constraint.pattern


我想为每个输入字段自定义此约束消息(即,相同形式的两个
nonEmptyText
约束将具有不同的约束消息)。我该怎么做呢?

这些消息是从消息中获取的。您可以创建自定义消息文件并将自定义文本放在其中。在源代码中导航以检查要放在那里的有效字符串

例如,
nonEmptyText
声明如下:

val nonEmptyText: Mapping[String] = text verifying Constraints.nonEmpty
从这里开始,
Constraints.nonEmpty
如下所示:

  def nonEmpty: Constraint[String] = Constraint[String]("constraint.required") { o =>
    if (o == null) Invalid(ValidationError("error.required")) else if (o.trim.isEmpty) Invalid(ValidationError("error.required")) else Valid
  }
def apply(message: String, args: Any*)
error.time=Expected time format is {0}
  def nonEmptyTextWithError(error: String): Mapping[String] = {
    Forms.text verifying Constraint[String]("constraint.required") { o =>
      if (o == null) Invalid(ValidationError(error)) else if (o.trim.isEmpty) Invalid(ValidationError(error)) else Valid
    }
  }
因此,错误字符串是“error.required”

现在,您可以在conf目录中创建一个文件
messages
,并在其中放置一行

error.required=This field is required
ValidationError
具有如下声明的
apply
方法:

  def nonEmpty: Constraint[String] = Constraint[String]("constraint.required") { o =>
    if (o == null) Invalid(ValidationError("error.required")) else if (o.trim.isEmpty) Invalid(ValidationError("error.required")) else Valid
  }
def apply(message: String, args: Any*)
error.time=Expected time format is {0}
  def nonEmptyTextWithError(error: String): Mapping[String] = {
    Forms.text verifying Constraint[String]("constraint.required") { o =>
      if (o == null) Invalid(ValidationError(error)) else if (o.trim.isEmpty) Invalid(ValidationError(error)) else Valid
    }
  }
这意味着您也可以在那里传递参数,在
消息中
您可以使用
{arg_num}
语法访问它们

例如,如果您创建了这样的错误

val format = ???
ValidationError("error.time", someFormat)
它将以有界形式返回,然后play将使用
MessagesApi
查找名为“error.time”的消息并相应地格式化,例如,您可以创建如下消息:

  def nonEmpty: Constraint[String] = Constraint[String]("constraint.required") { o =>
    if (o == null) Invalid(ValidationError("error.required")) else if (o.trim.isEmpty) Invalid(ValidationError("error.required")) else Valid
  }
def apply(message: String, args: Any*)
error.time=Expected time format is {0}
  def nonEmptyTextWithError(error: String): Mapping[String] = {
    Forms.text verifying Constraint[String]("constraint.required") { o =>
      if (o == null) Invalid(ValidationError(error)) else if (o.trim.isEmpty) Invalid(ValidationError(error)) else Valid
    }
  }
我的想法是为每个字段设置一条自定义消息,这是一种如下的自定义方法:

  def nonEmpty: Constraint[String] = Constraint[String]("constraint.required") { o =>
    if (o == null) Invalid(ValidationError("error.required")) else if (o.trim.isEmpty) Invalid(ValidationError("error.required")) else Valid
  }
def apply(message: String, args: Any*)
error.time=Expected time format is {0}
  def nonEmptyTextWithError(error: String): Mapping[String] = {
    Forms.text verifying Constraint[String]("constraint.required") { o =>
      if (o == null) Invalid(ValidationError(error)) else if (o.trim.isEmpty) Invalid(ValidationError(error)) else Valid
    }
  }

如果要使用多个约束子项,可能不是理想的解决方案。

是否可以使用
text
,而不是使用nonEmptyText,并将自定义消息放在
验证中,如下所示:

val voucherForm = Form(
  mapping(
    "voucherName" -> text.verifying(
      "Please specify a voucher name", f => f.trim!=""),
...

是 啊但这将为每个具有NoneEmptyText约束的输入框提供相同的消息。但我真正需要的是自定义错误。每个输入框所需的代码约束我认为您需要创建自己的约束。可能使用helper方法,我的意思是您可以重写nonEmptyText,但要使其包含一个将放入错误消息中的参数。嗯,有些约束嵌套得很深(例如,
number
constraint)。使用
“\u showConstraints->false
从模板中隐藏约束消息,并添加与自定义消息样式类似的html元素,这将更快、更干净。