2014-02-06 38 views
1

我得到play.api.data.Form [String]必需:選項[models.User]試圖顯示時錯誤的HTML視圖。scala:獲取play.api.data.Form [String]必需:選項[models.User]

下面是我正在嘗試使用的代碼,請隨時根據需要重新編輯所有內容。由於

控制器:

val forgotPasswordForm = Form(
    single(
     "email" -> text 
    ) verifying ("Invalid email or password", result => result match { 
     case (e) => User.findOneByEmail(e).isDefined 
    }) 
) 

    def forgotPassword() = withOptionUser { user => implicit request => 
    Ok(views.html.anon.forgotPassword(user)) 
    } 

    def forgotPasswordSubmit = withOptionUser { user: User => implicit request => 
    forgotPasswordForm.bindFromRequest().fold(
     formWithErrors => { 
     // do something with the bad form, like reshow the view 
     BadRequest(views.html.anon.forgotPassword(formWithErrors)) 
     }, 
     submission => { 
     // do something with the submitted form 
     Ok("got " + forgotPasswordForm.value) 
     } 
    ) 
    } 

HTML:

@( user: Option[User] 
) 

@stylesheet = { 
    <link rel="stylesheet" media="screen" href='@routes.Assets.at("styles/public/anon.min.css")'> 
} 

@main("Forgot Password",user: Option[User],"forgotPassword",stylesheet){ 

<section class="login"> 
    <div class="loginBox contentBox"> 
     <h1>Forgot Password?</h1> 

     @if(forgotPasswordForm.hasGlobalErrors) { 
      <ul> 
      @forgotPasswordForm.globalErrors.foreach { error => 
       <li>error.message</li> 
      } 
      </ul> 
     } 

     <p>Enter your email address:</p> 
     <form action="/forgotpassword" method="POST"> 
      <input type="email" name="email" placeholder="E-mail Address" id="email" data-xpl="loginEmail"/> 

      <div class="greenButton"> 
       <input type="submit" id="loginbutton" value="submit" data-xpl="loginSubmit"/> 
      </div> 
     </form> 
    </div> 
</section> 

} 
+0

感謝@biesior我雖然我們猜想,我們玩2+使用Scala沒有注意到錯標籤「回放」 – Monnster

+0

,你可以在tags/question中添加關於具體版本的信息。感謝biesior – biesior

+0

..會做到這一點 – Monnster

回答

1

formWithErrorsForm[String],和你的模板期待Option[User]。我不完全明白你爲什麼要將Option[User]傳遞給你忘記的密碼錶單,但該模板需要在參數列表中有forgotPasswordForm: Form[String]

然後你就可以使這樣的模板:

Ok(views.html.anon.forgotPassword(user, forgotPasswordForm)) 

BadRequest(views.html.anon.forgotPassword(user, formWithErrors)) 
相關問題