我有一个包含许多变量(姓名、电子邮件、密码等)的 User 类,有时我只需要更新其中的一两个变量(使用表单)。我从表单获取数据并使用 json 对象的绑定(bind):
Form<User> userForm = User.form.bind(json);
if(userForm.hasErrors()) {
return badRequest("error");
}
hasError()
检索错误,因为它没有接收到某些数据。我如何告诉 hasError()
它不必验证某些特定字段?
最佳答案
查看 GitHub 上的“Forms”示例应用程序。它展示了如何为您的约束分配一个“组”,以便您可以根据情况强制执行一组不同的约束。我参与的项目仍然在 Play 2.0.x 上,它没有这个功能,但我相信它在 Play 2.1 和/或 2.2 中。
https://github.com/playframework/playframework/tree/master/samples/java/forms
您要查看的文件是User
模型和Wizard
Controller 。
app/models/User.java
在模型中,您将约束分配给特定组。这些组似乎只是模型类中定义的接口(interface)。
@Required(groups = {All.class, Step1.class})
@MinLength(value = 4, groups = {All.class, Step1.class})
public String username;
app/controllers/Wizard.java
现在,当您进行表单绑定(bind)时,您将传入要验证的组/接口(interface)类。
Form<User> filledForm = form(User.class, User.Step1.class).bindFromRequest();
关于java - 有错误() : how to ignoring validation of specific input,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20073565/