1

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

1 に答える 1

0

ドキュメントによると、アドホック検証は「トップ」オブジェクトでのみ機能します。

http://www.playframework.com/documentation/2.2.x/JavaForms

フォームの場合、これはおそらくゲームではなくリストです。

于 2014-01-17T16:33:55.363 に答える