Play 2.5:找不到参数消息的隐含值:play.api.i18n.Messages

时间:2017-07-30 11:52:46

标签: forms scala playframework

我按照本主题的各种链接提供了所有答案:

Play 2.4: Form: could not find implicit value for parameter messages: play.api.i18n.Messages

could not find implicit value for parameter messages: play.api.i18n.Messages

然而,我无法通过这个错误:

app/views/user.scala.html:4: could not find implicit value for parameter messages: play.api.i18n.Messages

[error]   @helper.inputText(userForm("name"))
[error]                    ^
[error] app/views/user.scala.html:5: could not find implicit value for parameter messages: play.api.i18n.Messages
[error]   @helper.inputText(userForm("age"))

我正在关注此处的示例:https://www.playframework.com/documentation/2.5.x/ScalaForms

2 个答案:

答案 0 :(得分:0)

user.scala.html:

@(userForm: Form[UserData]) (implicit messages: Messages)

@helper.form(action = routes.UserController.userPost()) {
  @helper.inputText(userForm("name"))
  @helper.inputText(userForm("age"))
}

UserController.scala:

package controllers

import play.api.mvc._
import play.api.Play.current

import play.api.data._
import play.api.data.Forms._
import javax.inject.Inject
import play.api.i18n.I18nSupport
import play.api.i18n.MessagesApi
import play.api.i18n.Messages.Implicits._

import views._

case class UserData(name: String, age: Int)

class UserController @Inject()(val messagesApi: MessagesApi) extends Controller with I18nSupport {
   val userForm = Form(
     mapping(
       "name" -> text,
       "age" -> number
     )(UserData.apply)(UserData.unapply)
   )

   def index = Action {implicit request =>
     Ok(views.html.user(userForm))
   }
   def userPost = Action {implicit request =>
      userForm.bindFromRequest.fold(
        formWithErrors => {
          // binding failure, you retrieve the form containing errors:
          BadRequest(views.html.user(formWithErrors))
        },
        userData => {
         /* binding success, you get the actual value. */
         val name = userData.name
         val age = userData.age
         Ok(<message>Success !!</message>)
        }
      )
   }
}

答案 1 :(得分:0)

一切看起来都不错,除了以下之间的空间:

@(userForm: Form[UserData]) (implicit messages: Messages)

尝试:

@(userForm: Form[UserData])(implicit messages: Messages)

因为它应该是一个分组参数