Answer the question
In order to leave comments, you need to log in
How to deserialize an array that comes from a Symfony 4 form?
There is a form class, I send it with Ajax and the data array looks like this
name_form[fields]: value
I want to deserialize it all into an object
if ($request->isXmlHttpRequest()) {
$objectNormalizer = new ObjectNormalizer();
$normalizers = [$objectNormalizer];
$encoders = [new JsonEncoder()];
$serializer = new Serializer($normalizers, $encoders);
$data = $request->getContent();
$result = $serializer->deserialize($data, SiteCreateDto::class, 'json');
var_dump($result); exit;
}
Answer the question
In order to leave comments, you need to log in
I don’t understand why you create a serializer so strangely?
Firstly, ObjectNormalizer
it has a few more dependencies for its full-fledged work
. Secondly, it’s more correct to get it through DI.
public function submitAction(SerializerInterface $serializer, Request $request)
{
if ($request->isXmlHttpRequest()) {
$data = $request->getContent();
$result = $serializer->deserialize($data, SiteCreateDto::class, 'json');
var_dump($result); exit;
}
}
ObjectNormalizer
, then you will need to write your own: https://symfony.com/doc/current/serializer/custom_...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question