2016-06-30 14 views
1

ich ein Formular in Controller Authentication.scala haben:Spiel scala Tupel Form mit mehreren Fallklasse-Mapping in Ansichten

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

} 

Und ich habe einen Blick 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> 
} 
  • Ich weiß, dass ich das Formular nicht im Parameter für view.html.register() sende, aber die Frage ist, wie man den Import in den Ansichten implementiert, mit solch einem Formular? Ich bekomme ständig Fehler.

Hilfe sehr geschätzt. Danke

+0

Sollte der Typ Ihres Formulars im Template-Parameter nicht 'Form [(User, UserProfile)]' sein? – Mikesname

+0

@Mikesname das ist, was ich schon getan habe, und das funktioniert. Danke für die Antwort sowieso. Setzen Sie Ihren Vorschlag als Antwort, und ich werde es als akzeptierte Antwort markieren, ich könnte mir auch antworten, wie ich es herausfinde, aber, ich bin froh, dass jemand geantwortet hat, also werde ich Ihre Antwort ehren. – Maverick

Antwort

1

Wenn Sie ein Formular mit tuple(mapping(...), mapping(...)) erstellen, ist der Typ des Formulars ein Tupel der Mapping-Typen. In diesem Fall soll der Typ des Formularparameters der Ansicht Form[(User, UserProfile)] lauten.

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

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