という単純なエンティティがあり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()
);
}