如何在play framework 2.5.x中为表单inputText设置必需的属性

时间:2016-06-13 02:25:39

标签: forms playframework

我有一个用html写的输入:

<input type="text" class="form-control" placeholder="Title..." name="title" id="title" maxlength="255" data-error="Title must not be empty" required>

现在我想使用Helper.inputText替换上面的输入。这是我的代码:

helper.inputText(postForm("title"), 'class -> "form-control", 'placeholder -> "Title...", 'name -> "title", 'maxlength -> 255,
Symbol("data-error") -> "Title must not be empty")

我不知道如何为inputText设置必需的属性。 你有什么想法?谢谢!

1 个答案:

答案 0 :(得分:0)

约束在后端模型中定义。如果你正在使用Java,它将类似于

public class User {
    @Required
    protected String title;

    public void setTitle(String title) {
        this.title= title;
    }

    public String getTitle() {
        return title;
    }
}

详细信息:https://playframework.com/documentation/2.5.x/JavaForms#Defining-constraints

使用Scala:

val userForm = Form(
  mapping(
    "title" -> nonEmptyText
  )(UserData.apply)(UserData.unapply)
)

详细信息:https://playframework.com/documentation/2.5.x/ScalaForms#Defining-constraints-on-the-form