@@ -587,6 +587,99 @@ using the ``type`` option of the attribute::
587587
588588 The ``type `` option of ``#[MapRequestPayload] `` was introduced in Symfony 7.1.
589589
590+ .. _controller_map-uploaded-file :
591+
592+ Mapping Uploaded File
593+ ~~~~~~~~~~~~~~~~~~~~~
594+
595+ You can map ``UploadedFile``s to the controller arguments and optionally bind ``Constraints `` to them.
596+ The argument resolver fetches the ``UploadedFile `` based on the argument name.
597+
598+ ::
599+
600+ namespace App\Controller;
601+
602+ use Symfony\Component\HttpFoundation\File\UploadedFile;
603+ use Symfony\Component\HttpFoundation\Response;
604+ use Symfony\Component\HttpKernel\Attribute\MapUploadedFile;
605+ use Symfony\Component\Routing\Attribute\Route;
606+ use Symfony\Component\Validator\Constraints as Assert;
607+
608+ #[Route('/user/picture', methods: ['PUT'])]
609+ class ChangeUserPictureController
610+ {
611+ public function _invoke(
612+ #[MapUploadedFile([
613+ new Assert\File(mimeTypes: ['image/png', 'image/jpeg']),
614+ new Assert\Image(maxWidth: 3840, maxHeight: 2160)
615+ ])]
616+ UploadedFile $picture
617+ ): Response {
618+ // ...
619+ }
620+ }
621+
622+ .. tip ::
623+
624+ The bound ``Constraints `` are performed before injecting the ``UploadedFile `` into the controller argument.
625+ When a constraint violation is detected an ``HTTPException `` is thrown and the controller's
626+ action is not executed.
627+
628+ Mapping ``UploadedFile``s with no custom settings.
629+
630+ .. code-block:: php-attributes
631+
632+ #[MapUploadedFile]
633+ UploadedFile $document
634+
635+ An ``HTTPException `` is thrown when the file is not submitted.
636+ You can skip this check by making the controller argument nullable.
637+
638+ .. code-block :: php-attributes
639+
640+ #[MapUploadedFile]
641+ ?UploadedFile $document
642+
643+ .. code-block :: php-attributes
644+
645+ #[MapUploadedFile]
646+ UploadedFile|null $document
647+
648+ ``UploadedFile `` collections must be mapped to array or variadic arguments.
649+ The bound ``Constraints `` will be applied to each file in the collection.
650+ If a constraint violation is detected to one of them an ``HTTPException `` is thrown.
651+
652+ .. code-block :: php-attributes
653+
654+ #[MapUploadedFile(new Assert\File(mimeTypes: ['application/pdf']))]
655+ array $documents
656+
657+ .. code-block :: php-attributes
658+
659+ #[MapUploadedFile(new Assert\File(mimeTypes: ['application/pdf']))]
660+ UploadedFile ...$documents
661+
662+ Handling custom names.
663+
664+ .. code-block :: php-attributes
665+
666+ #[MapUploadedFile(name: 'something-else')]
667+ UploadedFile $document
668+
669+ Changing the ``HTTP Status `` thrown when constraint violations are detected.
670+
671+ .. code-block :: php-attributes
672+
673+ #[MapUploadedFile(
674+ constraints: new Assert\File(maxSize: '2M'),
675+ validationFailedStatusCode: Response::HTTP_REQUEST_ENTITY_TOO_LARGE
676+ )]
677+ UploadedFile $document
678+
679+ .. versionadded :: 7.1
680+
681+ The ``#[MapUploadedFile] `` attribute was introduced in Symfony 7.1.
682+
590683Managing the Session
591684--------------------
592685
0 commit comments