【问题标题】:Ad-Hoc Validation is not executed不执行临时验证
【发布时间】: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


    【解决方案1】:

    文档说临时验证仅适用于“顶部”对象。

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

    对于您的表单,这可能是列表,而不是游戏。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多