1
votes

I'm new to both Scala and Play, so figured I'd start simple with a basic form (following the Play tutorials). I've been trying to solve the following error for 2 days now:

could not find implicit value for parameter messages: play.api.i18n.Messages
formWithErrors => BadRequest(views.html.contact.index(formWithErrors))

This is the code I have so far:

Controller:

package controllers.contact

import play.api._
import play.api.mvc._
import models.contact.UserData

class ContactController extends Controller {
    def index = Action {
        Ok(views.html.contact.index(UserData.form))
    }

    def create = Action { implicit request =>
        UserData.form.bindFromRequest().fold(
                formWithErrors => BadRequest(views.html.contact.index(formWithErrors)),
                customer => Ok(s"Customer ${customer.name} created")
            )
    }
}

View:

@import play.api.data.Form
@import helper._
@import models.contact.UserData
@(form: Form[UserData])(implicit messages: Messages)

@main("") {
    @helper.form(action = controllers.contact.routes.ContactController.create()) {
        @helper.inputText(form("name"))
        @helper.inputText(form("age"))
        <input type="submit" value="Submit">
    }
}

Model:

package models.contact

import play.api.data._
import play.api.data.Forms._

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

object UserData {
    val form = Form(
            mapping (
                "name" -> text,
                "age" -> number
            )(UserData.apply)(UserData.unapply)
        )
}

Am I missing something painfully obvious? A shove in the right direction would be really appreciate

2

2 Answers

1
votes

Your template requires two parameter list

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

but you are passing only userData (formWithErrors) form

formWithErrors => BadRequest(views.html.contact.index(formWithErrors))

You need to either pass Message manualy as

formWithErrors => BadRequest(views.html.contact.index(formWithErrors)(message))

or declare message as implicit variable in scope as

implicit val message: Message = ...
1
votes

I think you can remove the second parameter list from your Form, as you don't use the parameter messages anywhere. That will solve your compile error.

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

Can be

@(form: Form[UserData])

If you are planning to use internationalized messages, you should make available an implicit val messages = ... in the scope where you call the view. The normal way of doing this is to put your messages in an external file, conf/messages and mix I18nSupport into your controller, which will provide the implicit messages value as described here.