0

I'm having trouble finding how to implement radio buttons for my form. I need to have 5 radionbuttons in that group representing a grade from 1 to 5.

Form:

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;
    }
}

Controller method:

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
    )

}

So my question is how do I setup the form in my controller and then make use of it in the view?

jakob
  • 5,979
  • 7
  • 64
  • 103

1 Answers1

3

First, your code has a mistake while rendering the form in controller.

The solution may look like this:

Controllers :

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);
    }
}

Views :

@(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">
    }
}

See documentation for inputRadioGroup helper here.

Moebius
  • 6,242
  • 7
  • 42
  • 54
Wayan Wiprayoga
  • 4,472
  • 4
  • 20
  • 30
  • In newer versions of Play - I'm on 2.2.x - you'll need to access form helpers through the `helper` package in your views. So, `@form` becomes `@helper.form`, `@inputRadioGroup` becomes `@helper. inputRadioGroup`, and `options = options(...)` becomes `options = @helper.options(...)`. – Dhruv Kapoor Apr 08 '14 at 19:17