Ad-Hoc验证未执行 [英] Ad-Hoc Validation is not executed
问题描述
我有一个名为 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 $中的自定义ad-hoc
验证
方法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屋!