Validation error message not generated properly

31 views
Skip to first unread message

Costa Basil

unread,
Dec 7, 2017, 3:53:35 PM12/7/17
to Play Framework

Hello:

I am using playframework 2.6.7/scala 2.12.4 with Jdk 1.8.0_151 on a macos sierra computer. My ide is IntellijIdea.

I have a form with a mapping that "firstName" -> nonEmptyText(1, 3). When I post a value that has more than 3 characters, the error message generate is what you see in the attachment:

Having WrappedArray(3) appear is wrong, it should be only 3. The problem is that the args sequence contains one element which is itself a WrappedArray.

I attached some debugging screenshots.

I think the problem occurs because out of all the apply mathods in MessagesImpl, the compiler picked the first apply with args: Any*, instead of the last one. So args, gets wrapped inside another array.

case class MessagesImpl(lang: Lang, messagesApi: MessagesApi) extends Messages {

  /**
   * Translates a message.
   *
   * Uses `java.text.MessageFormat` internally to format the message.
   *
   * @param key  the message key
   * @param args the message arguments
   * @return the formatted message or a default rendering if the key wasn’t defined
   */
  override def apply(key: String, args: Any*): String = {
    messagesApi(key, args: _*)(lang)
  }

  /**
   * Translates the first defined message.
   *
   * Uses `java.text.MessageFormat` internally to format the message.
   *
   * @param keys the message key
   * @param args the message arguments
   * @return the formatted message or a default rendering if the key wasn’t defined
   */
  override def apply(keys: Seq[String], args: Any*): String = {
    messagesApi(keys, args: _*)(lang)
  }

  /**
   * Translates a message.
   *
   * Uses `java.text.MessageFormat` internally to format the message.
   *
   * @param key  the message key
   * @param args the message arguments
   * @return the formatted message, if this key was defined
   */
  override def translate(key: String, args: Seq[Any]): Option[String] = {
    messagesApi.translate(key, args)(lang)
  }

How can I fix this? Is the bug in the scala compiler?

Thanks




Justin du coeur

unread,
Dec 7, 2017, 4:15:43 PM12/7/17
to play-fr...@googlegroups.com
On Thu, Dec 7, 2017 at 3:22 AM, Costa Basil <costa...@gmail.com> wrote:

I think the problem occurs because out of all the apply mathods in MessagesImpl, the compiler picked the first apply with args: Any*, instead of the last one. So args, gets wrapped inside another array.
... 
How can I fix this? Is the bug in the scala compiler?

Well, no -- that's almost certainly *correct* behavior as far as the compiler is concerned.  The question is, is this a bug in Play?  I don't know this code, but it smells like a missing :_* somewhere...

Costa Basil

unread,
Dec 8, 2017, 1:14:15 PM12/8/17
to Play Framework
You are right. It was late last night...

I think the bug is here:

case class FormError(key: String, messages: Seq[String], args: Seq[Any] = Nil) {

  def this(key: String, message: String) = this(key, Seq(message), Nil)

  def this(key: String, message: String, args: Seq[Any]) = this(key, Seq(message), args)

  lazy val message = messages.last

  /**
   * Copy this error with a new Message.
   *
   * @param message The new message.
   */
  def withMessage(message: String): FormError = FormError(key, message)

  /**
   * Displays the formatted message, for use in a template.
   */
  def format(implicit messages: play.api.i18n.Messages): String = {
    messages.apply(message, args) <-- missing :_*

Costa Basil

unread,
Dec 8, 2017, 1:14:15 PM12/8/17
to Play Framework
Reply all
Reply to author
Forward
0 new messages