【问题标题】:Java example of inputRadioGroup in Play2Play2 中 inputRadioGroup 的 Java 示例
【发布时间】:2013-02-24 12:27:55
【问题描述】:

我无法找到如何为我的表单实现单选按钮。我需要在该组中有 5 个单选按钮,代表从 1 到 5 的等级。

表格:

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
    )

}

所以我的问题是如何在我的控制器中设置表单,然后在视图中使用它?

【问题讨论】:

    标签: java forms playframework playframework-2.1


    【解决方案1】:

    首先,您的代码在控制器中呈现表单时出错。

    解决方案可能如下所示:

    控制器:

    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 helper here 的文档。

    【讨论】:

    • 在较新版本的 Play 中 - 我在 2.2.x 上 - 您需要通过视图中的 helper 包访问表单助手。所以,@form 变为 @helper.form@inputRadioGroup 变为 @helper. inputRadioGroupoptions = options(...) 变为 options = @helper.options(...)
    猜你喜欢
    • 1970-01-01
    • 2014-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-23
    • 1970-01-01
    相关资源
    最近更新 更多