Ad-Hoc Validation is not executed

放肆的年华 提交于 2019-12-23 03:09:45

问题


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.

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.

This is the Game Entity:

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

This is the Form that contains multiple Game Entities.

public class GameForm {

    @Valid
    public List<Game> games;

    public GameForm()
    {
        games = new ArrayList<Game>();
    }   
}

This is the controller method.

public static Result save()
{
    Form<GameForm> gameForm = form(GameForm.class).bindFromRequest();

    if(gameForm.hasErrors())
        return badRequest(create.render(gameForm));

    return redirect(
        routes.Games.index()
    );
}

回答1:


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

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

For your Form this is probably List, not Game.



来源:https://stackoverflow.com/questions/19741421/ad-hoc-validation-is-not-executed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!