Home » Php » php – Symfony 3 – How to handle JSON request with a form

php – Symfony 3 – How to handle JSON request with a form

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’m having a hard time figuring out how to handle a JSON request with Symfony forms (using v3.0.1).

Here is my controller:

/**
 * @Route("/tablet")
 * @Method("POST")
 */
public function tabletAction(Request $request)
{
    $tablet = new Tablet();
    $form = $this->createForm(ApiTabletType::class, $tablet);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($tablet);
        $em->flush();
    }

    return new Response('');
}

And my form:

class ApiTabletType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('macAddress')
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => 'AppBundle\Entity\Tablet'
        ]);
    }
}

When I send a POST request with the Content-Type header properly set to application/json, my form is invalid… all fields are null.

Here is the exception message I get if I comment the if ($form->isValid()) line :

An exception occurred while executing 'INSERT INTO tablet
(mac_address, site_id) VALUES (?, ?)' with params [null, null]:

I’ve tried sending different JSON with the same result each time:

  • {"id":"9","macAddress":"5E:FF:56:A2:AF:15"}
  • {"api_tablet":{"id":"9","macAddress":"5E:FF:56:A2:AF:15"}}

“api_tablet” being what getBlockPrefix returns (Symfony 3 equivalent to form types getName method in Symfony 2).

Can anyone tell me what I’ve been doing wrong?


UPDATE:

I tried overriding getBlockPrefix in my form type. The form fields have no prefix anymore, but still no luck :/

public function getBlockPrefix()
{
    return '';
}
How to&Answers:
$data = json_decode($request->getContent(), true);
$form->submit($data);

if ($form->isValid()) {
    // and so on…
}

Answer:

I guess you can drop forms and populate->validate entities

$jsonData = $request->getContent();
$serializer->deserialize($jsonData, Entity::class, 'json', [
    'object_to_populate' => $entity,
]);
$violations = $validator->validate($entity);

if (!$violations->count()) {

    return Response('Valid entity');
}

return new Response('Invalid entity');

https://symfony.com/doc/current/components/serializer.html#deserializing-in-an-existing-object

https://symfony.com/components/Validator

Answer:

I would recommend you use the FOSRestBundle.

Here’s an example config I use at the moment:

fos_rest:
    view:
        view_response_listener: force
        force_redirects:
          html: true
        formats:
            jsonp: true
            json: true
            xml: false
            rss: false
        templating_formats:
            html: true
        jsonp_handler: ~
    body_listener: true
    param_fetcher_listener: force
    allowed_methods_listener: true
    access_denied_listener:
        json: true
    format_listener:
        enabled: true
        rules:
            - { path: ^/, priorities: [ 'html', 'json' ], fallback_format: html, prefer_extension: true }
    routing_loader:
            default_format: ~
            include_format: true
    exception:
        enabled: true
        codes:
            'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404
            'Doctrine\ORM\OptimisticLockException': HTTP_CONFLICT
        messages:
            'Symfony\Component\Routing\Exception\ResourceNotFoundException': true

Make sure you have getBlockPrefix defined with something (I haven’t tried empty strings so it might work):

public function getBlockPrefix()
{
   return 'api_tablet'
}

Disable CSRF protection for good measure:

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'csrf_protection' => false,
        'data_class' => 'AppBundle\Entity\Tablet'
    ]);
}

You can then POST the following data to the form:

{"api_tablet":{"macAddress":"5E:FF:56:A2:AF:15"}}

Notes / caveats:

  • You need to make sure your Form is configured with all the fields of
    the Entity. You can configure what you need in a validator.

  • You have to post in camelCase. FOSRestBundle supports Array
    Normalization, which I haven’t tried, but apparently that will let
    you post in underscores.