3
votes

I am using playframework with scala and i am trying to build form but getting following error

"An implicit MessagesProvider instance was not found. Please see https://www.playframework.com/documentation/2.6.x/ScalaForms#Passing-MessagesProvider-to-Form-Helpers" here is my index.scala.html

@(customerForm:Form[Customer])

@import helper._

@main("welcome") {
    <h1>Customer Form</h1>
    @form(action=routes.Application.createCustomer()) {
        @inputText(customerForm("Credit Limit"))
        @inputText(customerForm("Customer Name"))
        <input type="submit" value="Submit">
    }
}

And this is my application controller code

package controllers

import play.api._
import play.api.mvc._
import models.Customer
import play.api.data._
import play.api.data.Forms._

class Application extends Controller {

  def customerForm = Form(mapping("Customer Name" -> nonEmptyText,
    "Credit Limit" -> number)(Customer.apply)(Customer.unapply))

  def index = Action { implicit request =>
    Ok(views.html.index(customerForm))
  }

  def createCustomer = Action { implicit request =>
    customerForm.bindFromRequest().fold(
      formWithErrors => BadRequest(views.html.index(formWithErrors)),
      customer => Ok(s"Customer ${customer.name} created successfully"))
  }

}
2
Have you read the documentation (the link that is in the error you mentionned) ? playframework.com/documentation/2.6.x/… - Dnomyar

2 Answers

14
votes

The play framework forms handling has changed between version 2.5 and 2.6, in order to make thing works you have to change the declaration of your Application class as follow :

import javax.inject._
import play.api.i18n.I18nSupport

class Application @Inject()(val cc: ControllerComponents) extends AbstractController(cc) with I18nSupport

and in your view add an implicit parametre as follow :

@(customerForm:Form[Customer])(implicit request: RequestHeader, messagesProvider: MessagesProvider)

If you dont need the RequestHeader in your view you may omit its declaration.

Please refer to the link in your error message for more information : https://www.playframework.com/documentation/2.6.x/ScalaForms#Passing-MessagesProvider-to-Form-Helpers

1
votes

I got the same error and in my case, I forgot to make request implicit. I hope this answer helps if you change the code as Achraf answered but still have the error.

def foo() = Action { implicit request =>
    Ok(html.index(form))
}