Ad-Hoc验证未执行 [英] Ad-Hoc Validation is not executed

查看:114
本文介绍了Ad-Hoc验证未执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 Game 的简单实体。我想允许我的用户一次编辑多个这些实体。因此,我需要一个包含多个游戏实体的表单。

I have a simple entity called Game. I want to allow my users to edit multiple of these entities at once. Therefore I need a form that contains multiple Game Entities.

问题:提交表单时我调用 hasErrors() 游戏验证方法c>实体永远不会被调用。只检查由注释标记的验证,并在它们无效时产生错误。

The problem: When the form is submitted and I invoke hasErrors() my custom ad-hoc validate method in the Game entities is never called. Only the validations marked by annotations are checked and produce errors when they are invalid.

这是游戏实体:

@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);
}

这是包含多个游戏的表格实体。

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()
    );
}


推荐答案

文档说广告 - 临时验证仅适用于顶层对象。

The docs say that ad-hoc validation only works on the "top" object.

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

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

For your Form this is probably List, not Game.

这篇关于Ad-Hoc验证未执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆