【问题标题】:Play Framework - how to change the label of a input when using form helpers?Play Framework - 使用表单助手时如何更改输入的标签?
【发布时间】:2018-06-27 12:09:15
【问题描述】:

如果我使用form 助手在我的站点上获取loginForm,如何自定义输入标签?当然,设置的标签是我的 Java 类中变量的名称。但是,我想要另一个标签名称而不是变量名称。

在这一点上,我发现改变它的唯一方法是设置一个全新的表单构建器。在此,我查看我要说明的元素是否是我要设置新标签然后设置另一个标签的元素。但是,我认为这太过分了。

我可以在@helper.input 语句的末尾添加一个简单的变量来更改标签名称吗?

我正在使用 PlayFramework 2.6。

【问题讨论】:

    标签: forms scala web playframework


    【解决方案1】:

    是的。您只需要在我们的输入中添加一个'_label 参数。例如:

    @(userForm: Form[User])
    
    @helper.form(action = routes.UsersController.save()) {
        @helper.inputText(userForm("name"), '_label -> "Your name")
        @helper.inputText(userForm("email"), '_label -> "Your E-mail")
    }
    

    注意它只有在开头有一个'。这记录在这里:

    https://playframework.com/documentation/2.6.x/JavaFormHelpers

    如果您需要高度自定义生成 HTML,另一种方法是通过 handling HTML input creation yourself

    【讨论】:

    • 非常感谢,这是一个非常好的答案。我看到了这个,但是认为这些只适合 Formbuilder :)
    猜你喜欢
    • 1970-01-01
    • 2013-04-30
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    • 1970-01-01
    • 2019-09-26
    相关资源
    最近更新 更多