user51
user51

Reputation: 10143

could not find implicit value for parameter messages: play.api.i18n.Messages in Play Framework 2.5.3

I am trying to test the sample path variables in my local using Play Framework 2.5.3.

 package controllers

 import play.api._
 import play.api.data._
 import play.api.data.Forms._
 import play.api.mvc._
 import play.api.libs.json._
 import play.api.libs.json.Json._
 import play.api.libs.functional.syntax._

 case class Product(sku: String, title: String)
 object Product {
implicit def pathBinder(implicit stringBinder: PathBindable[String]) = new PathBindable[Product] {
  override def bind(key: String, value: String): Either[String, Product]    = {
    for {
      sku <- stringBinder.bind(key, value).right
      product <- productMap.get(sku).toRight("Product not found").right
    } yield product
  }
  override def unbind(key: String, product: Product): String = {
    stringBinder.unbind(key, product.sku)
  }
 }

def add(product: Product) = productMap += (product.sku -> product)

val productMap = scala.collection.mutable.Map(
    "ABC" -> Product("ABC", "8-Port Switch"),
    "DEF" -> Product("DEF", "16-Port Switch"),
    "GHI" -> Product("GHI", "24-Port Switch")
)
}

object Products extends Controller {
  implicit val productWrites = new Writes[Product] {
  def writes(product: Product) = Json.obj(
    "sku" -> product.sku,
    "title" -> product.title
    )
  }

implicit val productReads: Reads[Product] = (
(JsPath \ "sku").read[String] and
(JsPath \ "title").read[String]
)(Product.apply _)

val productForm: Form[Product] = Form(
  mapping(
    "sku" -> nonEmptyText,
    "title" -> nonEmptyText
  )(Product.apply)(Product.unapply)
)

def index = Action {
    Ok(toJson(Product.productMap.values))
}

def postProduct = Action(BodyParsers.parse.json) { request =>
    val post = request.body.validate[Product]
  post.fold(
    errors => {
      BadRequest(Json.obj("status" ->"error", "message" -> JsError.toFlatJson(errors)))
    },
    product => { 
        Product.add(product)
      Created(toJson(product))
    }
  )
}

def edit(product: Product) = Action {
    Ok(views.html.products.form(product.sku, productForm.fill(product)))
}

def update(sku: String) = Action {
    Ok("Received update request")
}

}

And below is my view.

  @(sku: String, productForm: Form[controllers.Product])

  @helper.form(action = routes.Products.update(sku)) {
  @helper.inputText(productForm("sku"))
  @helper.inputText(productForm("title"))
  } 

When I am compiling this program I am getting below error

 [error] /home/rajkumar/CodeBase/workspaceEclipse/PFC2/app/views/products/form.scala.html:4: could not find implicit value for parameter messages: play.api.i18n.Messages
  [error]   @helper.inputText(productForm("sku"))
   [error]                    ^
  [error] /home/rajkumar/CodeBase/workspaceEclipse/PFC2/app/views/products/form.scala.html:5: could not find implicit value for parameter messages: play.api.i18n.Messages
  [error]   @helper.inputText(productForm("title"))
   [error]                    ^
  [error] 9 errors found
  [error] (compile:compileIncremental) Compilation failed

Could anyone help me resolving this issue.

Upvotes: 0

Views: 2730

Answers (2)

user267817
user267817

Reputation:

As @jirka-helmich pointed out, the solution by @jerry is deprecated.

The Play 2.5.x documentation lists the following solution (note the I18nSupport trait):

import javax.inject.Inject
import play.api.i18n.I18nSupport
class MyController @Inject()(val messagesApi: MessagesApi) extends Controller with I18nSupport {
// ...

See this answer for more details.

Upvotes: 1

Jerry
Jerry

Reputation: 492

when you use inputText, the 'implicit messages' is needed. Add it by importing the following statements

import play.api.Play.current
import play.api.i18n.Messages.Implicits._

In the views you will need to add the following code

@(sku: String, productForm: Form[controllers.Product])(implicit messages: Messages)

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

Upvotes: 0

Related Questions