在视图中使用多个案例类映射来播放scala元组表单

时间:2016-06-30 09:31:41

标签: scala playframework playframework-2.5

我在控制器Authentication.scala中有一个表单:

      package controllers

import javax.inject._
import play.api._
import play.api.mvc._
import play.api.data._
import play.api.data.Forms._
import play.api.i18n.Messages.Implicits._
import play.api.libs.concurrent.Execution.Implicits.defaultContext

import models.User
import models.UserProfile


/**
 * This controller creates an `Action` to handle HTTP requests to the
 * application's home page.
 */
@Singleton
class Authentication @Inject() extends Controller {

  val registerForm = Form(
    tuple(
      "user" -> mapping(
        "email" -> nonEmptyText,
        "password" -> nonEmptyText
        )(User.apply)(User.unapply),
      "profile" -> mapping(
        "firstname"->nonEmptyText,
        "lastname"->nonEmptyText,
        "gender" -> ignored(0)
      )(UserProfile.apply)(UserProfile.unapply))
    )

  /**
   * Create an Action to render an HTML page with a welcome message.
   * The configuration in the `routes` file means that this method
   * will be called when the application receives a `GET` request with
   * a path of `/`.
   */
  def login = Action {
    Ok(views.html.login())
  }

  def loginSubmit = Action {
    implicit request => 
    // val maybeFoo = request.body.asFormUrlEncoded.get("password").lift(0) // returns an Option[String]
    // val something = maybeFoo map {_.toString} getOrElse 0
    // println(something)
    Ok("Hello")
  }

  def register = Action{
    Ok(views.html.register())
  }

  def registerSubmit = Action{
    implicit request =>
    registerForm.bindFromRequest.fold(
      formWithErrors => {
        // binding failure, you retrieve the form containing errors:
        println(formWithErrors)
        BadRequest(views.html.register())
      },
      userData => {
        /* binding success, you get the actual value. */
        Redirect(routes.Authentication.register())
      }
    )
  }

  def forgotPassword = Action{
    Ok(views.html.forgot_password())
  }

}

我有一个视图register.scala.html:

    @import b3.vertical.fieldConstructor
@(userForm: Form[tuple(Mapping, Mapping)])(implicit messages: Messages)
@main("Register") {
    <!-- REGISTRATION FORM -->
<div class="text-center" style="padding:50px 0">
    <div class="logo">register</div>
    <!-- Main Form -->
    <div class="login-form-1">
        <form id="register-form" class="text-left" method="POST" action="@routes.Authentication.registerSubmit">
            <div class="login-form-main-message"></div>
            <div class="main-login-form">
                <div class="login-group">
                    <div class="form-group">
                        <label for="reg_email" class="sr-only">Email</label>
                        <input type="text" class="form-control" id="reg_email" name="email" placeholder="email">
                    </div>
                    <div class="form-group">
                        <label for="reg_password" class="sr-only">Password</label>
                        <input type="password" class="form-control" id="reg_password" name="password" placeholder="password">
                    </div>
                    <div class="form-group">
                        <label for="password_confirm" class="sr-only">Password Confirm</label>
                        <input type="password" class="form-control" id="password_confirm" name="reg_password_confirm" placeholder="confirm password">
                    </div>


                    <div class="form-group">
                        <label for="firstname" class="sr-only">First Name</label>
                        <input type="text" class="form-control" id="firstname" name="firstname" placeholder="First Name">
                    </div>

                    <div class="form-group">
                        <label for="lastname" class="sr-only">Last Name</label>
                        <input type="text" class="form-control" id="lastname" name="lastname" placeholder="Last Name">
                    </div>

                    <div class="form-group login-group-checkbox">
                        <input type="radio" class="" name="gender" id="male" placeholder="username">
                        <label for="male">male</label>

                        <input type="radio" class="" name="gender" id="female" placeholder="username">
                        <label for="female">female</label>
                    </div>

                    <div class="form-group login-group-checkbox">
                        <input type="checkbox" class="" id="reg_agree" name="reg_agree">
                        <label for="reg_agree">i agree with <a href="#">terms</a></label>
                    </div>
                </div>
                <button type="submit" class="login-button"><i class="fa fa-chevron-right"></i></button>
            </div>
            <div class="etc-login-form">
                <p>already have an account? <a href="#">login here</a></p>
            </div>
        </form>
    </div>
    <!-- end:Main Form -->
</div>
}
  • 我知道我没有在view.html.register()的参数中发送表单,但问题是如何在视图中实现导入,使用这样的表单?我一直都在收错。

非常感谢。感谢

1 个答案:

答案 0 :(得分:1)

使用tuple(mapping(...), mapping(...))创建表单时,表单的类型是映射类型的元组。因此,在这种情况下,您希望视图的表单参数类型为Form[(User, UserProfile)]

@(userForm: Form[(User, UserProfile)])(implicit messages: Messages)

@main("Register") {
    ... HTML ...
}