2013-02-24 5 views
0

양식에 라디오 버튼을 구현하는 방법을 찾는 데 어려움이 있습니다. 나는 1에 등급을 나타내는 해당 그룹의 5 radionbuttons이 필요 5.Play2의 inputRadioGroup에 대한 Java 예제

형태 :

public static class MobileWriteReview { 

    @Constraints.MinLength(1) 
    @Constraints.MaxLength(32) 
    @Constraints.Required 
    public String firstName; 

    @Constraints.MinLength(1) 
    @Constraints.MaxLength(32) 
    @Constraints.Required 
    public String lastName; 

    @Constraints.MinLength(5) 
    @Constraints.Required 
    public String password; 

    @Constraints.MinLength(5) 
    @Constraints.Required 
    public String repeatPassword; 

    @Constraints.Required 
    public int grade; 

    @Constraints.MinLength(30) 
    @Constraints.Required 
    public String text; 

    /** 
    * Required by play. 
    */ 
    public MobileWriteReview() { 
    } 

    public MobileWriteReview(int grade) { 
     this.grade = grade; 
    } 
} 

컨트롤러 방법 :

private static final Form<MobileWriteReview> MOBILE_WRITE_REVIEW_FORM = form(MobileWriteReview.class); 

public static Result review(){ 
     MobileWriteReview mobileWriteReview = new MobileWriteReview(3); 
     MOBILE_WRITE_REVIEW_FORM.fill(mobileWriteReview); 
     return ok(mobileInviteToReview.render(MOBILE_WRITE_REVIEW_FORM)); 
    } 

public static Result doReview(){ 
     final Form<MobileWriteReview> filledForm = MOBILE_WRITE_REVIEW_FORM.bindFromRequest(); 
     if (filledForm.hasErrors()) { 
      // User did not fill everything properly 
      return badRequest(mobileInviteToReview.render(filledForm)); 
     } else { 
      // Everything was filled 
      return ok(); 
     } 
} 

view.scala.html

@(inviteForm: Form[_]) 
@styles = { 

} 
@scripts = { 

} 

@import helper._ 

@helper.form(routes.MobileInviteToReview.doReview) { 

    @if(inviteForm.hasGlobalErrors) { 
    <p class="error"> 
     <span class="label label-important">@inviteForm.globalError.message</span> 
    </p> 
    } 

    @inputRadioGroup(
    // appropriate code here 
    ) 

}

제 질문은 어떻게하면 내 컨트롤러에서 폼을 설정하고 뷰에서 사용할 수 있습니까?

답변

2

먼저 컨트롤러에서 양식을 렌더링하는 동안 코드에 실수가 있습니다.

솔루션은 다음과 같을 수 있습니다

컨트롤러 :

private static final Form<MobileWriteReview> MOBILE_WRITE_REVIEW_FORM = Form.form(MobileWriteReview.class); // this field declared as final 

public static Result review(){ 
    MobileWriteReview mobileWriteReview = new MobileWriteReview(4); 
    Logger.info("Mobile Write Review grade = " + mobileWriteReview.grade); 

    // this is proper way to fill the form using existing value 
    return ok(views.html.mobileInviteToReview.render(MOBILE_WRITE_REVIEW_FORM.fill(mobileWriteReview))); 
} 

public static Result doReview(){ 
    final Form<MobileWriteReview> filledForm = MOBILE_WRITE_REVIEW_FORM.bindFromRequest(); 
    MobileWriteReview mobileWriteReview = filledForm.get(); 
    Logger.info("Grade submitted = " + mobileWriteReview.grade); 

    if (filledForm.hasErrors()) { 
     // User did not fill everything properly 
     return badRequest(views.html.mobileInviteToReview.render(filledForm)); 
    } else { 
     // Everything was filled 
     return ok("Grade submitted = " + mobileWriteReview.grade); 
    } 
} 

조회수 :

@(inviteForm: Form[models.MobileWriteReview]) 
@import views.html.helper._ 

@main(title = "Input Radio Group Sample") { 
    @form(action = routes.Application.doReview()) { 
     @****** This helper can accomodate selected value of radio button if present *****@ 
     @inputRadioGroup(
     inviteForm("grade"), 
     options = options("1" -> "1", "2" -> "2", "3" -> "3", "4" -> "4", "5" -> "5") 
    ) 

     <input type="submit" value="Post"> 
    } 
} 

참조 문서 inputRadioGroup 도우미에 대한 10.

+0

최신 버전의 Play에서는 2.2.x입니다.보기에서 'helper' 패키지를 통해 양식 헬퍼에 액세스해야합니다. 따라서'@ form'은'@ helper.form'이되고,'@ inputRadioGroup'은'@helper '가됩니다. inputRadioGroup'이고'options = options (...)'는'options = @ helper.options (...)'가됩니다. –