Symfony 3 - How to handle JSON request with a form

Solution 1

$data = json_decode($request->getContent(), true);
$form->submit($data);

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

Solution 2

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

Share:
Admin
Author by

Admin

Updated on June 12, 2022

Comments

  • Admin
    Admin about 2 months

Related