5

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.

Here is what I've got in my POST controller action:

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

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

    $form->submit($data);

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

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

    return $form;
}

What I do is:

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

The questions I have:

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

-

$form->handleRequest($request)
if ($form->isValid()) {
    ...
}
...

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

  1. Is there any advantage of using the Form Component over using a ParamConverter together with the validator directly - here is the idea:

-

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

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

    $this->persistAndFlush($guest);

    return ....;
}

Thanks!

luqo33
  • 8,001
  • 16
  • 54
  • 107

1 Answers1

0

I'm trying to do the same thing to you and it's difficult to find some answers for this subject. I think today it's an important subject to develop website with api rest for mobile apps. Anyway!

Please find my answer below:

Do I really always have to json_decode() the Request content manually before submitting it to a Form?

No, i get data like this $params = $request->query->all(); $user->setUsername($params['fos_user_registration_form']['username']);

Can this process be somehow automated with FosRestBundle?

I don't think so.

Is it possible to send application/x-www-form-urlencoded data to the controller action and have it handled with

Yes, but i'm currently trying to handle my form like that and I can not do it.

Is there any advantage of using the Form Component over using a ParamConverter together with the validator directly - here is the idea:

No

I'm guessing if I'm not going to only register the user with:
$userManager = $this->get('fos_user.user_manager');

and make my control manually.

I posted an issue here and i'm waiting: https://github.com/FriendsOfSymfony/FOSUserBundle/issues/2405

did you manage to get further information ?

iometrine
  • 41
  • 11