Ad-Hocvalidation未执行
我有一个名为Game
的简单实体。 我想允许我的用户一次编辑多个这些实体。 因此,我需要一个包含多个Game
实体的表单。
问题:当提交表单并调用hasErrors()
我的Game
实体中的自定义ad-hoc validate
方法永远不会被调用。 仅检查由注释标记的validation,并在它们无效时产生错误。
这是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 find = new Model.Finder(Long.class, Game.class); }
这是包含多个Game
实体的表单。
public class GameForm { @Valid public List games; public GameForm() { games = new ArrayList(); } }
这是控制器方法。
public static Result save() { Form gameForm = form(GameForm.class).bindFromRequest(); if(gameForm.hasErrors()) return badRequest(create.render(gameForm)); return redirect( routes.Games.index() ); }
文档说ad-hocvalidation仅适用于“顶层”对象。
http://www.playframework.com/documentation/2.2.x/JavaForms
对于你的表格,这可能是List,而不是Game。