gpt4 book ai didi

scala - 使用 Twitter Bootstrap 字段 Play Framework 表单验证消息

转载 作者:行者123 更新时间:2023-12-04 16:04:27 26 4
gpt4 key购买 nike

我正在尝试更改显示在使用 Play 框架创建的表单中的验证消息的文本

enter image description here

'Required'、'Numeric'、'Date(yyyy...)' 等文本会根据绑定(bind)到表单字段的属性类型自动显示。

@implicitFieldConstructor = @{
FieldConstructor(twitterBootstrapInput.f)
}

<fieldset>
@inputText(productForm("id"), '_label -> "Product id")
@inputText(productForm("info.name"), '_label -> "Name", 'placeholder -> "Product Name")
@inputText(productForm("info.code"), '_label -> "Code")
@inputText(productForm("info.weight"), '_label -> "Weight")
@inputDate(productForm("info.dateAdded"), '_label -> "Date Added")
</fieldset>

一切都很好,但我想知道如何更改它。是否可以通过在 @inputText 元素上使用某些属性,例如。 smth like '_helpinline -> "Please enter the name" (我确实找过这样的smth但找不到任何东西),也许人们还有其他使用方式?

这些消息显示在 Twitter bootstrap help-inline 风格的元素中。

最佳答案

虽然可以通过传递 message 轻松更改错误消息约束注释的参数(如 @Required(message = "abc") ),对于这些信息而言并非如此。实际上,这就是定义它们的地方:在一个注解中,约束注解本身被注解(如此元)。就see source如果我解释得太难了。注意 @play.data.Form.Display(name="constraint.required")行。

那么,我们可以用它做什么?

选项 1:如果您想为 ALL @Required 字段更改此选项。如您所见,此注释定义的不是消息本身,而是其本地化键。因此,您可以在本地化中覆盖它,只需在 conf/messages.en 中添加以下行即可:

constraint.required=This is so very required!

然后到所有其他语言文件。

选项 2:如果您希望每个输入都有不同的消息作为参数。那么你根本不需要进入这个验证机制,你可以只修改你的字段构造函数Here is the source内置 twitter bootstrap 字段构造函数。通过复制它来制作你自己的(如果你还没有),然后将你想要的所有内容放入 <span class="help-inline"> .例如,处理 '_helpinline -> "Please enter the name" ,第 14 行可能如下:

<span class="help-inline">
@elements.args.get('_helpinline).getOrElse( elements.errors(elements.lang).mkString(", ") )
</span>

你看,我把初始值放到了getOrElse中, 所以如果你不提供任何 '_helpinline使用默认信息。

选项 3:如果您希望每个输入都有不同的消息,通过注释自定义就像 @Required(message = "abc") . 然后你可能想制作你自己的自定义@Require 注释,方法是从the source 复制粘贴它。并进行一些修改。这是最棘手的选项,我认为选项 2 更好,但了解如何制作自己的验证器注释仍然很有用,因为它并不像看起来那么难。

希望这些变体中的一些会有所帮助。实际上,我打赌 2 日 :)

关于scala - 使用 Twitter Bootstrap 字段 Play Framework 表单验证消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18381242/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com