Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update form-data.md to check input and validator #1759

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions core/form-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
}
Expand Down