Search code examples
scalaplayframeworkplay-authenticate

Scala Play 2.5 Form convention and implicit Messages (MessagesApi) access


I'm working on this Scala Play application and after researching and thinking for a bit ended favoring a design that places all forms under a form package at the level where they are used in the views (or the topmost level that applies) e.g.

 app
    | views 
           | account
                    | form (all Form used by account will be here)
                          | PasswordChangeForm.scala

Then the PasswordChangeForm.scala form is implemented as:

package views.account.form

import play.api.data.Form
import play.api.data.Forms.{mapping, text}
import play.api.i18n.Messages

case class PasswordChange(password: String, repeatPassword: String)

object PasswordChangeForm {
  val Instance = Form {
    mapping(
      "password" -> text(minLength = 5),
      "repeatPassword" -> text(minLength = 5)
    )(PasswordChange.apply)(PasswordChange.unapply).
      verifying(Messages("playauthenticate.change_password.error.passwords_not_same"),
        data => data.password != null && !data.password.isEmpty && data.password.equals(data.repeatPassword))
  }
}

The problem is I don't see how I can make Messages or better MessagesApi available to the forms for the error reporting.

The compiler error is as expected could not find implicit value for parameter messages: play.api.i18n.Messages:

[error] /home/bravegag/code/play-authenticate-usage-scala/app/views/account/form/PasswordChangeForm.scala:15: could not find implicit value for parameter messages: play.api.i18n.Messages
[error]       verifying(Messages("playauthenticate.change_password.error.passwords_not_same"),

UPDATE A possibility is to refactor the solution above from:

val Instance = Form { 

to

def create(implicit messages: Messages) = Form {

but then it will create a new instance of the Form each time.


Solution

  • Make your PasswordChangeForm a singleton class and Inject MessagesApi using guice dependency Injection.

    @Singleton
    class PasswordChangeForm @Inject() (messages: MessagesApi) {
      //now use it like this messages("somekey")
    }
    

    usage:

    messages("somekey")
    

    The above structure is singleton and is ensured by guice. Guice injects messages api during the initialisation of PasswordChangeForm.