2012-08-26 3 views
6

저는 Play2를 처음 사용합니다 (저는 이미 Play1을 사용하여 프로젝트를 개발했습니다). 요청에 따라 양식 바인딩에 문제가 있습니다. 양식에 대한 문서는 정말 가볍습니다.Play Framework 2 요청서에서 바인딩 양식

private final static Form<Estimation> estimationForm = form(Estimation.class); 

/** 
* Get an estimation by form 
* @return 
*/ 
public static Result estimation() { 
    return ok(views.html.rate.estimation.render(
     estimationForm, 
     City.findAll() 
    )); 
} 

/** 
* Display estimation results 
* @return 
*/ 
public static Result results() { 
    if (request().method().equals("POST")) { 
     Form<Estimation> form = estimationForm.bindFromRequest(); 
     if (form.hasErrors()) { 
      System.out.println(form.errorsAsJson().toString()); 
      return ok(views.html.rate.estimation.render(
       form 
       City.findAll() 
      )); 
     } 
     else { 
      System.out.println(form.get()); 
      return ok(views.html.rate.results.render(

      )); 
     } 
    } 
    else { 
     return estimation(); 
    } 
} 

나는이 같은 선택에 도시를 표시 :

<select id="city" name="city">  
    <option value="1">Paris, France</option> 
    <option value="2">Lyon, France</option> 
    <option value="3">Marseille, France</option> 
    <option value="4">Barcelona, Spain</option> 
    <option value="5">Berlin, Germany</option> 
</select> 

내가 양식을 제출하면, 나는 다음과 같은 오류가 있습니다 을 { "여기

내 컨트롤러의 코드 "도시": [ "잘못된 값"]}

그래서 여기 내 질문입니다 : 바인더가 간단한 필드 (예 : 내 모델의 String 속성)와 잘 작동하는 것 같지만, @Man yToOne 관계?

감사합니다.

답변

9

선택 필드의 이름을 name="city.id"

+0

으로 설정하십시오. 답변 해 주셔서 감사합니다. 내가 말한대로 코드를 수정했지만 더 좋지 않습니다. form.toString()을 인쇄하면 다음과 같습니다. 양식 (of = 클래스 models.analytic.Estimation, 데이터 = {금액 = 125, city.id = 1}, 값 = 일부 (null >> 125.0) , errors = {}) 하지만 Form.Obget() 객체를 저장하려고 할 때 City 필드의 NotNull 제약 조건에 대한 유효성 검사 오류가 있습니다 ... 혹시 궁금한 점이 있습니까? 다시 고마워;) – c4k

+0

죄송합니다. 조사가 끝나면 내 문제가 해결됩니다. 사실, 나는 form.get() 객체를 직접 저장하려고 시도했지만, 값이 항상 null이되도록 폼에 필요하지 않은 빈에 필드가 있습니다. 엔터티를 저장하기 전에 컨트롤러에서이 필드를 설정하여 문제를 해결했습니다 (이 문제가있는 다른 사용자에게 도움이 될 수있는 경우).) – c4k

관련 문제