当我提交表单时,playframework 会触发验证,但在 pojo 类中我没有定义任何Annotations
验证。验证仅在两个字段上触发。我的代码如下:
POJO类:
public class User {
public int id;
public String name;
public int age;
public User() {}
public User(int id, String name, int age){
this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
我的控制器方法:
public Result saveUser() {
Form<User> userData = userForm.bindFromRequest();
if(userData.hasErrors()){
flash("error", "Please enter valid values");
return badRequest(add_user.render(userData));
}
User user = userData.get();
boolean result = userService.addNewUser(user);
if(result){
flash("success", String.format("Add user success %s", user));
}else{
flash("error", String.format("Fail to save user, Please contact admin %s", user));
}
return redirect(routes.UserController.newUsers());
}
HTML 表单:
@(userForm: Form[com.harmeetsingh13.entities.User])
@import helper._
@import helper.twitterBootstrap._
@main("New User"){
<h1 style="margin-left: 20px;">User Form</h1>
@helper.form(action = com.harmeetsingh13.controllers.routes.UserController.saveUser(),
'id -> "userForm", 'style -> "margin-left: 20px;"){
<fieldset>
<legend>User (@userForm("name").valueOr("New"))</legend>
@helper.inputText(userForm("id"), '_label -> "ID")
@helper.inputText(userForm("name"), '_label -> "Name")
@helper.inputText(userForm("age"), '_label -> "Age")
</fieldset>
<input type="submit" class="btn btn-primary" value="Save" />
}
}
当我提交表单时,我收到以下错误: