1
votes

May I ask your help with Play framework of v2.6.13. I trying to validate query string parameters and therefore I've implemented following QueryStringBindable:

import play.api.data.validation._ 
import play.api.i18n.Messages 
import play.api.mvc.QueryStringBindable
import scala.util.{Failure, Success, Try}

case class PagingParams(offset: Int, limit: Int)

object PagingParams {

  val DefaultOffset = 1
  val DefaultLimit = 20   
  val OffsetParamKey = "offset"   
  val LimitParamKey = "limit"   
  val ValidationConstraints = Seq(Constraints.min(1), Constraints.max(100000))

  implicit def queryStringBinder(implicit intBinder: QueryStringBindable[Int]) = new QueryStringBindable[PagingParams] {

    override def bind(key: String, params: Map[String, Seq[String]]): Option[Either[String, PagingParams]] = {
      val result = for {
        offset <- Try(intBinder.bind(OffsetParamKey, params).get).recover {
          case _ => Right(DefaultOffset)
        }
        limit <- Try(intBinder.bind(LimitParamKey, params).get).recover {
          case _ => Right(DefaultLimit)
        }
      } yield {
        (offset.right.toOption, limit.right.toOption)
      }
      result match {
        case Success((maybeIndex, maybeLimit)) =>
          ParameterValidator(ValidationConstraints, maybeIndex, maybeLimit) match {
            case Valid =>
              Some(Right(PagingParams(maybeLimit.get, maybeLimit.get)))
            case Invalid(errors) =>
              Some(Left(errors.zip(Seq(OffsetParamKey, LimitParamKey)).map {
                case (ValidationError(message, value), param) => Messages(message, param, value)
              }.mkString(", ")))
          }
        case Failure(e) => Some(Left(s"Invalid paging params: ${e.getMessage}"))
      }
    }

    override def unbind(key: String, pagingParams: PagingParams): String =
      intBinder.unbind(OffsetParamKey, pagingParams.offset) + "&" + intBinder.unbind(LimitParamKey, pagingParams.limit)   
  } 
}

Problem is that running application I'm getting following error:

An implicit MessagesProvider instance was not found.

Unfortunately I can't find any help of how can I implicitly inject MessagesProvider so that I can retrieve messages from messages file.

Here is how my endpoint looks like in routes file:

GET     /table                      controllers.TableController.index(pagingParams: PagingParams)

Thank you very much. Please accept my apologies if similar question has been asked already.

1

1 Answers

0
votes

As shown in the Play documentation, your controller class should inject (you could import com.google.inject.Inject for injection) ControllerComponents, extends AbstractController and I18nSupport:

class MyController  @Inject()(cc: ControllerComponents)
  extends AbstractController(cc) with play.api.i18n.I18nSupport { 
    ??? //your controller methods
} 

Also you need to use requestHeader and MessageProvider instances, within your views file:

@(viewsElement: String)(implicit request: RequestHeader, messagesProvider: MessagesProvider)