0
votes

I have a problem with custom operation in ApiPlatform. I can't get forms submission under any circumstances. I don't know what I'm doing wrong.

Im using Postman (https://www.getpostman.com/) for testing the api. I tried every method: application/x-www-form-urlencoded, form-data, raw (application/json). I allso change the route and necessary setup for GET method, and tried to send parameter "state" in query string.

ApiPlatform standard post operations are working so i think that this excludes server configuration.

Api platform config:

api_platform:
    formats:
        json: { mime_types: [ "application/json" ] }
        jsonld: { mime_types: [ "application/ld+json" ] }
        form: { mime_types: [ "application/x-www-form-urlencoded" ] }
    mapping:
        paths: ['%kernel.project_dir%/src/Entity']

Nelmio cors config:

nelmio_cors:
    defaults:
        origin_regex: true
        allow_origin: ['%env(CORS_ALLOW_ORIGIN)%']
        allow_methods: ['GET', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE']
        allow_headers: ['Content-Type', 'Authorization']
        expose_headers: ['Link']
        max_age: 3600
    paths:
        '^/': null

ApiResource entity config:

 * @ApiResource(
 *     collectionOperations={
 *         "get",
 *         "post"={
 *             "method"="POST",
 *             "path"="/assets",
 *             "controller"=UploadAssetAction::class,
 *             "defaults"={"_api_receive"=false},
 *         }
 *     }
 * )

My custom operation action:

namespace App\Controller;

use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;

class UploadAssetAction
{
    /** @var FormFactoryInterface */
    private $formFactory;

    public function __construct(FormFactoryInterface $formFactory)
    {
        $this->formFactory = $formFactory;
    }

    public function __invoke(Request $request)
    {

        /**
         * State parameter is in request stack.
         */
        var_dump($request);

        $form = $this->formFactory->createBuilder()
            ->add('state', TextType::class)
            ->getForm();

        $form->handleRequest($request);

        if ($form->isSubmitted()) {
            die('Form submitted');
        }

        die('No form submission');
    }
}

I did also tried with custom form class.

The result is allways the same var_dump of entire request and "No form submission" at the end.

1

1 Answers

0
votes

Maybe try this (if json):

public function __invoke(Request $request)
{
    [...]

    $form->handleRequest(json_decode($request->getContent(), true));

    [...]
}