Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The reason the long handed version works is because scala's automatic type inference triggers an implicit conversion <code>toJsFieldJsValueWrapper</code>.</p> <p>For example</p> <pre><code>scala&gt; import play.api.libs.json._ import play.api.libs.functional.syntax._ import play.api.libs.json._ scala&gt; import play.api.libs.functional.syntax._ import play.api.libs.functional.syntax._ scala&gt; import Json._ import Json._ scala&gt; arr("invalid", "tooshort") res0: play.api.libs.json.JsArray = ["invalid","tooshort"] scala&gt; obj("name" -&gt; arr("invalid", "tooshort")) res1: play.api.libs.json.JsObject = {"name":["invalid","tooshort"]} scala&gt; obj _ res3: Seq[(String, play.api.libs.json.Json.JsValueWrapper)] =&gt; play.api.libs.json.JsObject = &lt;function1&gt; </code></pre> <p>Notice that <code>arr</code> returns a <code>JsArray</code>, however <code>obj</code> requires a <code>JsValueWrapper</code>. Scala is able to convert the <code>JsArray</code> to <code>JsValueWrapper</code> when it constructs the arguments for <code>obj</code>. However it is not able to convert a <code>Seq[(String, JsArray)]</code> to a `Seq[(String, JsValueWrapper)].</p> <p>If you provide the expected type when of the sequence in advance, the Scala compiler's type inferencer will perform the conversion as it creates the sequence.</p> <pre><code>scala&gt; Seq[(String, JsValueWrapper)]("name" -&gt; arr("invalid", "tooshort"), "email" -&gt; arr("invalid")) res4: Seq[(String, play.api.libs.json.Json.JsValueWrapper)] = List((name,JsValueWrapperImpl(["invalid","tooshort"])), (email,JsValueWrapperImpl(["invalid"]))) </code></pre> <p>However once the sequence is created it cannot be converted unless there is an implicit conversion in scope that can convert sequences.</p> <p>The final code snippet looks like:</p> <pre><code>def error = Action { val e = Seq[(String, JsValueWrapper)]("name" -&gt; arr("invalid", "tooshort"), "email" -&gt; arr("invalid")) // in real app it will be Seq[(JsPath, Seq[ValidationError])] BadRequest(obj( "errors" -&gt; obj(e: _*) )) } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload