执行了 Playframework 临时表单验证,但未按预期工作
Playframework ad-hoc form validation is executed but does not work as it suppose
我有 Login
class 和 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
)
我得到了好的页面,就像没有错误一样,但在验证方法中我已经声明了它。
我用的是play 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()
方法并在那里进行检查(假设您的 Customer
class 中有一个 id
字段):
public String validate() {
return Customer.id != null ? null : "Oh no, the customer id is empty";
}
P.S。同时拥有 public 字段和 setters/getters 有点令人困惑 - 切换到 public 字段或将它们设为私有并使用 getter 和 setter
我有 Login
class 和 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
)
我得到了好的页面,就像没有错误一样,但在验证方法中我已经声明了它。
我用的是play 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()
方法并在那里进行检查(假设您的 Customer
class 中有一个 id
字段):
public String validate() {
return Customer.id != null ? null : "Oh no, the customer id is empty";
}
P.S。同时拥有 public 字段和 setters/getters 有点令人困惑 - 切换到 public 字段或将它们设为私有并使用 getter 和 setter