Handling POST requests with Symofny Forms in FOSRestBundle
<p>It's not clear to me how I should use the Symfony Form Component with FOSRestBundle for POST endpoints that I use to create resources.</p>

<p>Here is what I've got in my POST controller action:</p>

<pre><code>//GuestController.php
public function cpostAction(Request $request)
{
    $data = json_decode($request-&gt;getContent(), true);

    $entity = new Guest();
    $form = $this-&gt;createForm(GuestType::class, $entity);

    $form-&gt;submit($data);

    if ($form-&gt;isValid()) {
        $dm = $this-&gt;getDoctrine()-&gt;getManager();
        $dm-&gt;persist($entity);
        $dm-&gt;flush();

        return new Response('', Response::HTTP_CREATED);
    }

    return $form;
}
</code></pre>

<p>What I do is:</p>

<ol>
<li>Send an <code>application/json</code> POST request to the endpoint (<code>/guests</code>);</li>
<li>Create a form instance that binds to an entity (<code>Guest</code>);</li>
<li>Due to the fact that I'm sending JSON, I need to <code>json_decode</code> the request body before submitting it to the form (<code>$form-&gt;submit($data)</code>).</li>
</ol>

<p><strong>The questions I have:</strong></p>

<ol>
<li>Do I really always have to <code>json_decode()</code> the <code>Request</code> content manually before submitting it to a Form? Can this process be somehow automated with FosRestBundle?</li>
<li>Is it possible to send <code>application/x-www-form-urlencoded</code> data to the controller action and have it handled with:</li>
</ol>

<p>-</p>

<pre><code>$form-&gt;handleRequest($request)
if ($form-&gt;isValid()) {
    ...
}
...
</code></pre>

<p>I couldn't get the above to work, the form instance was never submitted.</p>

<ol start="3">
<li>Is there any advantage of using the Form Component over using a <code>ParamConverter</code> together with the validator directly - here is the idea:</li>
</ol>

<p>-</p>

<pre><code>/**
 * @ParamConverter("guest", converter="fos_rest.request_body")
*/
public function cpostAction(Guest $guest)
{
    $violations = $this-&gt;getValidator()-&gt;validate($guest);

    if ($violations-&gt;count()) {
        return $this-&gt;view($violations, Codes::HTTP_BAD_REQUEST);
    }

    $this-&gt;persistAndFlush($guest);

    return ....;
}
</code></pre>

<p>Thanks!</p>