0

I've got two models with 1:n relation and want to use form validators.

If I choose an option from the select, the correct id of the element is stored on the database.
But if I leave it -- Choose a game--, NULL is stored although the game property is annotated with @Constraints.Required.

Models

@Entity
public class Server extends Model {

    @Id
    public Integer serverId;

    @ManyToOne
    @Constraints.Required
    public Game game;

    ...
}
@Entity
public class Game extends Model {

    @Id
    @Constraints.Required
    public Integer gameId;

    @Constraints.Required
    public String name;

    public static Map<String,String> options() {
        LinkedHashMap<String,String> options = new LinkedHashMap<String,String>();

        for(Game c: Game.find.orderBy("name").findList()) {
            options.put(c.gameId.toString(), c.name);
        }

        return options;
    }

    ...
}

Controller

public static Result create() {
    return ok(views.html.Server.create.render(form(models.Server.class)))
}

Template

@helper.select(form("game.gameId"), helper.options(Game.options), '_default -> "-- Choose a game --")

I've already tried to use a @Valid annotation to force play.data.Form to validate the Game-Model during the binding process, but that only causes a simple evaluation of the constraints on Game properties, which are not filled. Although a correct game is choosen from the list, it gives me an error that name is empty:

@Entity
public class Server extends Model {

    @ManyToOne
    @Constraints.Required
    @Valid
    public Game game;

    @Constraints.Required
    public String name;

...

Thanks for your help.

twes
  • 307
  • 4
  • 9

1 Answers1

0

I fixed this problem by writing a validate() method like this:

public boolean validate() {
    return Game.gameId != null
}

I also looked into the Spring Binding documentation, because Play2.0 Java uses the Spring Binder. In chapter 17.2.4.11 (link) they're giving the default option the value "-". Haven't tested it, but it might be the solution.

Kaijiro
  • 339
  • 1
  • 3
  • 14
Leonard Punt
  • 1,051
  • 9
  • 17
  • 1
    Using a `validate()` method would work if global errors should be created, not if I want field related errors? Are there any other abilities to run custom validation and display errors beside the input elements? – twes Apr 20 '12 at 15:19
  • You can, of course, write your own method(s) and call them from your controller. It is possible to add errors to the Form object, by calling the `reject("key", "error")` method. – Leonard Punt Apr 20 '12 at 18:53
  • Perfect. This is very helpful and suits my needs. – twes Apr 20 '12 at 20:34
  • Doing explicit validation of separate fields in the controller would couple it to the model. And besides, validating the relation (that is: ensure that game.gameId is a number and referring to an existing Game) should be a quite common task. There should be a more straight-forward way of doing this! – Markus Aug 30 '12 at 10:53