【问题标题】:Play 2.5: could not find implicit value for parameter messages: play.api.i18n.Messages播放 2.5:找不到参数消息的隐式值:play.api.i18n.Messages
【发布时间】:2018-01-06 01:24:33
【问题描述】:

我遵循了有关该主题的各种链接中提供的所有答案:

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

【问题讨论】:

  • 请向我们展示user.scala.html和您的控制器的内容
  • documentation 中所示“您应该像这样在模板中添加消息隐式参数:@()(implicit messages: Messages)
  • 发布内容如下。

标签: forms scala playframework


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    试试:

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

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

    【讨论】:

    • 天哪!那行得通。非常感谢。但令人费解的是,空间可以在不给出任何提示的情况下引起问题。并且没有关于此播放限制的文档。
    • 在 Play 框架中哪里可以找到这种与语法相关的限制?我相信这是 Play 而不是 Scala 的限制。
    猜你喜欢
    • 2015-08-28
    • 2016-11-08
    • 2016-09-19
    • 2016-12-16
    • 2016-02-22
    • 1970-01-01
    • 2016-06-21
    • 2017-02-02
    • 2016-01-17
    相关资源
    最近更新 更多