【发布时间】:2013-11-02 11:29:06
【问题描述】:
我有一个名为Game 的简单实体。我想允许我的用户一次编辑多个这些实体。因此我需要一个包含多个Game 实体的表单。
问题:当提交表单并且我调用 hasErrors() 我在 Game 实体中的自定义临时 validate 方法时,永远不会被调用。只有注解标记的验证被检查并在无效时产生错误。
这是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()
);
}
【问题讨论】:
标签: java playframework playframework-2.0