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 回答