Note that there are some explanatory texts on larger screens.

plurals
  1. POForm binding with custom mapping to object - how?
    text
    copied!<p>I have these case class</p> <pre><code>case class Blog(id:Long, author:User, other stuff...) case class Comment(id:Long, blog:Blog, comment:String) </code></pre> <p>and a form on the client side that submits the data</p> <pre><code>blog_id:"5" comment:"wasssup" </code></pre> <p>I'm writing some simple code to let a user add a comment to a blog.<br> The user is logged in so the his <code>user_id</code> is not needed from the client side, we know who he is... </p> <p>I would like to bind the <code>blog_id</code> to a <code>Blog</code> object loaded from db, and if it doesn't exist show an error.<br> The examples on play framework docs are not helpful.<br> They only show mappings for forms that represent a single Object and all of its fields.<br> Here I'm representing a tuple of a <code>(b:Blog, comment:String)</code> and for the <code>Blog</code> I'm only supplying it's <code>id</code>. </p> <p>I'd like to have a mapping that would provide me with the <strong>conversion + validation + error messages</strong>, so i can write something like: </p> <pre><code>val form = Form( tuple( "blog_id" -&gt; blogMapping, "comment" -&gt; nonEmptyText ) ) form.bindFromRequest().fold(... formWithErrors =&gt; {... }, { case (blog, comment) =&gt; {do some db stuff to create the comment} ... </code></pre> <p>The "blogMapping" wlil work like other mappings, it will bind the posted data to an object, in our case a blog loaded from db, and in case it's not successful it will provide an error that we can use on the <code>formWithErrors =&gt;</code> clause.</p> <p>I'm not sure how to accomplish this, the docs are kinda lacking here...<br> any help is appreciated!</p>
 

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