在视图中播放带有多个案例类映射的scala元组形式

小牛

我在控制器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()的参数中发送表单,但问题是如何使用这样的表单在视图中实现导入?我一直都在犯错误。

帮助非常感谢。谢谢

迈克斯名

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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Scala中带有行为的案例类

来自分类Dev

带有 CircularFifoQueue 的 Scala 案例类

来自分类Dev

播放带有框架案例声明的框架Scala模板

来自分类Dev

选择多个案例

来自分类Dev

使用多个案例是 <>

来自分类Dev

在Signup视图中以“ CreateView”的形式处理表单,并带有多个值

来自分类常见问题

在Scala中比较2个案例类实例

来自分类Dev

在Scala中比较2个案例类实例

来自分类Dev

Scala中带有案例类的工厂设计模式

来自分类Dev

Scala中带有案例类的工厂设计模式

来自分类Dev

Scala:过滤带有选项字段的案例类列表

来自分类Dev

Haskell中具有相同说明的多个案例

来自分类Dev

switch语句中具有相同别名的多个案例

来自分类Dev

当有多个案例时,将und case和_mutate

来自分类Dev

模式匹配具有相同动作的多个案例

来自分类Dev

SML:多个案例陈述

来自分类Dev

使用多个案例条件

来自分类Dev

木偶-带有控制器的多个视图中的数据

来自分类Dev

在一个案例中有多个选项,然后在案例中循环

来自分类Dev

是否可以在scala中具有不同名称和略有不同的字段的两个案例类之间进行转换

来自分类Dev

在提升后的映射中将案例类包含在另一个案例中

来自分类Dev

通过展开Scala 3中的类型将案例类转换为另一个案例类

来自分类Dev

Scala:带有元组的flatMap

来自分类Dev

scala-数据集-如何在两个案例类上使用函数?

来自分类Dev

过滤列表到 scala 数据集中每个参数值的前 2 个案例类

来自分类Dev

将一个案例类转换为具有相同结构但具有一个附加字段的另一个案例类

来自分类Dev

案例类,持久性和播放形式

来自分类Dev

MATLAB:在 switch 中运行多个案例

来自分类Dev

我如何选择多个案例

Related 相关文章

热门标签

归档