问题描述:

I want to make a ReST infrastructure with a Symfony server (using FOSRestBundle).

there is

  • a Contact entity, (id, name, email)
  • a AddType form to add a new contact

On the client side, I have a form which sends a POST request whose body is

{"the_name_of_my contact_form":{"name":"foo", "email": "[email protected]"}}

My controller (which extends FOSRestController) can see the data in the request

$request->request->get($form->getName()) returns {"name":"foo", "email": "[email protected]"}

But whether I use $form->handleRequest($request) or $form->submit($data)

$form->isValid() is always false

I hope this is clear enough... can anyone help?

网友答案:

This problem is related to the CRSF validation. You have to disable it for the user requesting the service. You can disable it in you config.yml. You'll have to set something like this:

fos_rest:
    disable_csrf_role: ROLE_API

Just make sure that the user requesting your service has this role. You can read more about user roles here.

Also, you'll have to submit you form, not handle it. Here is an snippet of it:

    $form = $this->createForm(ProjectType::class, $project);
    $form->submit($request->request->all());

    if ($form->isValid()) {
相关阅读:
Top