我有方法如下的Login
类validate
:
public static class Login {
/** The customer. */
@ManyToOne
@Constraints.Required
public Customer customer;
/** The password. */
public String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Customer getCustomer() {
return this.customer;
}
public void setCustomer(Customer c) {
this.customer = c;
}
/**
* Validate.
*
* @return the string
*/
@Transactional
public String validate() {
return "Global error";
}
}
表单绑定的代码:
Form<Login> filledLoginForm = form(Login.class);
filledLoginForm.bindFromRequest();
当我验证表单如下时:
if (filledLoginForm.hasGlobalErrors()) {
return badRequest(views.html.login.render(filledLoginForm));
} else if (filledLoginForm.hasErrors()) {
return badRequest(views.html.login.render(filledLoginForm));
} else {
return ok("OK");
}
看法:
<input type="hidden" id="customer_id" name="customer.id" value="@customer.id" />
@inputPassword(
LoginForm("password"),
'_label -> "Hasło",
'_showConstraints -> false,
'_showErrors -> false
)
我得到了确定的页面,就像没有错误一样,但是在validate方法中我已经声明了它。
我使用播放2.2.6
我建议您在控制器中明确检查此内容。就像是:
final String cId = form().bindFromRequest().get("customer.id");
if (filledLoginForm.hasGlobalErrors()
|| cId==null || cId.equals("")) {
// in this case either there were some validation errors or the provided customer.id is not valid
return badRequest(yourTemplateHere.render(filledLoginForm));
}
您可以尝试的另validate()
一种方法是稍微重写一下您的方法并在那里进行检查(假设id
您的Customer
类中有一个字段):
public String validate() {
return Customer.id != null ? null : "Oh no, the customer id is empty";
}
同时具有公共字段和设置器/获取器的PS有点混乱-切换到公共字段或将其设为私有并使用获取器和设置器
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句