4

I am trying to build a form with Play Framework 2, the usual syntax is:

@helper.form(action = routes.Application.submit, 'id -> "myForm") {   
}

Note that the single quotation mark is before id is opened and never closed.

Is there another syntax that I can use to do the same thing?

Vitaly Olegovitch
  • 3,509
  • 6
  • 33
  • 49

3 Answers3

4

The 'id is a Symbol.

You could use the Symbol("sym") syntax if you don't like this one, but it is not standard.

scala> 'symbol == Symbol("symbol")
res0: Boolean = true
Marth
  • 23,920
  • 3
  • 60
  • 72
  • Thank you! I guess that it's like writing `new String("string")` instead of `"string"`, but it is much less disturbing and compatible with most common editors. – Vitaly Olegovitch Mar 03 '15 at 11:10
3

You could work around it with an implicit conversion. This will require using a scala source file, though (seems like you're using java, but you can mix them).

app/libs/SymbolImplicits.scala

package example.libs

object SymbolImplicits {
    implicit def string2Symbol[A](s: (String, A)): (Symbol, A) = (Symbol(s._1), s._2)
}

Then in your view you would @import example.libs.SymbolImplicits._, so you can then do:

@helper.form(action = routes.Application.submit, "id" -> "myForm") {   
}

"id" -> "myForm" is then implicitly converted to 'id -> "myForm".

To avoid using that import in every view, you could also add this line to build.sbt (or in Build.scala project properties) instead:

TwirlKeys.templateImports += "example.libs.SymbolImplicits._"
Michael Zajac
  • 55,144
  • 7
  • 113
  • 138
1

No, that's required syntax for Scala's Symbol as pointed in other answer, anyway except that it looks weird for it's perfectly valid and there's no reason to fight with it.

Community
  • 1
  • 1
biesior
  • 55,576
  • 10
  • 125
  • 182