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


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);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();

    return new Response('');

And my form:

class ApiTabletType extends AbstractType
    public function buildForm(FormBuilderInterface $builder, array $options)

    public function configureOptions(OptionsResolver $resolver)
            '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?


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);

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


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');




I would recommend you use the FOSRestBundle.

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

        view_response_listener: force
          html: true
            jsonp: true
            json: true
            xml: false
            rss: false
            html: true
        jsonp_handler: ~
    body_listener: true
    param_fetcher_listener: force
    allowed_methods_listener: true
        json: true
        enabled: true
            - { path: ^/, priorities: [ 'html', 'json' ], fallback_format: html, prefer_extension: true }
            default_format: ~
            include_format: true
        enabled: true
            'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404
            'Doctrine\ORM\OptimisticLockException': HTTP_CONFLICT
            '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)
        'csrf_protection' => false,
        'data_class' => 'AppBundle\Entity\Tablet'

You can then POST the following data to the form:


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.