As temporary solution I used form.data()
map to get int values of selected id's and than fetch objects from database by this values. But again I think, that it's not the best solution.
. . .
Map<String, String> formData = form.data();
String schoolId = formData.get("school.id");
Integer id = Integer.parseInt(schoolId);
if (id == null) {
return badRequest("Incorrect school identifier: " + schoolId);
}
School scl = School.find.byId(id);
if (scl == null) {
return notFound("Can't found school with id " + schoolId);
}
String specialityId = formData.get("speciality.id");
id = Integer.parseInt(specialityId);
if (id == null) {
return badRequest("Incorrect speciality identifier: " + specialityId);
}
Speciality spc = Speciality.find.byId(id);
if (spc == null) {
return notFound("Can't found speciality with id " + specialityId);
}
Card card = form.get();
card.person = p;
card.school = scl;
card.speciality = spc;
. . .
Also, I have used
. . .
@helper.form(...) {
@helper.select(itemForm("school.id"), options(...), ...)
@helper.select(itemForm("speciality.id"), options(...), ...)
. . . }
instead of
. . .
@helper.form(...) {
@helper.select(itemForm("school"), options(...), ...)
@helper.select(itemForm("speciality"), options(...), ...)
. . . }
in the edit.scala.html file to solve validation errors.
P.S. Changing declaration of id
field in the model objects makes me feel better. Now I need less code to save Card
instance. Only
. . .
Card card = form.get();
card.person = p;
. . .
instead of example before.