我有一个简单的实体,名为Game
. 我想允许我的用户一次编辑多个这些实体。因此我需要一个包含多个Game
实体的表单。
问题:当提交表单并且我在实体中调用hasErrors()
我的自定义临时validate
方法时,从未调用过。Game
仅检查由注释标记的验证,并在无效时产生错误。
这是Game
实体:
@Entity
public class Game extends Model {
@Id
public Long id;
@ManyToOne
@Constraints.Required
public Team team1;
@ManyToOne
@Constraints.Required
public Team team2;
//the validate method does not get called
public String validate()
{
System.out.println("Validating the Game Entity.");
if(team1.id == team2.id)
return "You have to choose two different teams!";
return null;
}
public static Model.Finder<Long,Game> find = new Model.Finder<Long,Game>(Long.class, Game.class);
}
这是包含多个Game
实体的表单。
public class GameForm {
@Valid
public List<Game> games;
public GameForm()
{
games = new ArrayList<Game>();
}
}
这是控制器方法。
public static Result save()
{
Form<GameForm> gameForm = form(GameForm.class).bindFromRequest();
if(gameForm.hasErrors())
return badRequest(create.render(gameForm));
return redirect(
routes.Games.index()
);
}