リレーションを持つ 2 つのモデルが1:n
あり、フォーム バリデーターを使用したいと考えています。
選択からオプションを選択すると、要素の正しい ID がデータベースに保存されます。
しかし、そのままにしておくと、ゲームのプロパティに注釈が付けられていますが-- Choose a game--
、が保存されます。NULL
@Constraints.Required
モデル
@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;
}
...
}
コントローラ
public static Result create() {
return ok(views.html.Server.create.render(form(models.Server.class)))
}
テンプレート
@helper.select(form("game.gameId"), helper.options(Game.options), '_default -> "-- Choose a game --")
バインド プロセス中に -Modelを強制的に検証するために@Valid
注釈を使用しようとしましたが、プロパティの制約が単純に評価されるだけで、満たされていません。リストから正しいゲームが選択されていますが、空のエラーが表示されます。play.data.Form
Game
Game
name
@Entity
public class Server extends Model {
@ManyToOne
@Constraints.Required
@Valid
public Game game;
@Constraints.Required
public String name;
...
ご協力いただきありがとうございます。