문제

I am trying Play framework 2.2.2 and again I am confused. I have an object (Card.java):

@Entity
public class Card extends Model {

    private static final long serialVersionUID = 1L;
    @Id
    public final int id;
    @ManyToOne
    public Person person; // Card owner
    @ManyToOne
    public Speciality speciality; // Card Speciality
    public Date beginDate; // Begin education date
    public Date endDate; // end education date
    @ManyToOne
    public School school;
        . . .
}

Also, I have a form like this (edit.scala.html):

@(itemForm: Form[models.entities.Card], person: models.entities.Person, id: Int)
@import helper._
@main("Personal card") {
@helper.form(action = routes.Cards.save(person.id, id)) {
@helper.select(itemForm("school"), options(models.entities.Person.schools()),'_label -> "School name",'_showConstraints -> false)
@helper.select(itemForm("speciality"), options(models.entities.Person.specialities()),'_label -> "Speciality name",'_showConstraints -> false)
. . . }
}

After filing form and submitting it, I get validation error for school and speciality fields. I think it's becouse type mismatch (integer and School for example).

Is anybody has example such case of forms? How can I pass validation and set object field having only integer key value?

Best regards and thanks for wasting your time for me.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top