diff --git a/core/form-data.md b/core/form-data.md index b159e6f0b2b..18acdd92b45 100644 --- a/core/form-data.md +++ b/core/form-data.md @@ -17,24 +17,33 @@ This decorator is able to denormalize posted form data to the target object. In namespace App\EventListener; + use ApiPlatform\Serializer\SerializerContextBuilderInterface; use ApiPlatform\Symfony\EventListener\DeserializeListener as DecoratedListener; use ApiPlatform\Util\RequestAttributesExtractor; +use ApiPlatform\Validator\ValidatorInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Event\RequestEvent; use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; final class DeserializeListener { - private $decorated; - private $denormalizer; - private $serializerContextBuilder; - - public function __construct(DenormalizerInterface $denormalizer, SerializerContextBuilderInterface $serializerContextBuilder, DecoratedListener $decorated) + private DecoratedListener $decorated; + private DenormalizerInterface $denormalizer; + private SerializerContextBuilderInterface $serializerContextBuilder; + private ValidatorInterface $validator; + + public function __construct( + DenormalizerInterface $denormalizer, + SerializerContextBuilderInterface $serializerContextBuilder, + DecoratedListener $decorated, + ValidatorInterface $validator + ) { $this->denormalizer = $denormalizer; $this->serializerContextBuilder = $serializerContextBuilder; $this->decorated = $decorated; + $this->validator = $validator; } public function onKernelRequest(RequestEvent $event): void { @@ -63,7 +72,10 @@ final class DeserializeListener } $data = $request->request->all(); - $object = $this->denormalizer->denormalize($data, $attributes['resource_class'], null, $context); + $input = $context["input"]["class"] ?? $attributes['resource_class']; + $object = $this->denormalizer->denormalize($data, $input, null, $context); + $this->validator->validate($object); + $request->attributes->set('data', $object); } }