Skip to content

Commit 15e622c

Browse files
committed
Add documentation for #[MapUploadedFile] attribute
1 parent f7225d9 commit 15e622c

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

controller.rst

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,94 @@ using the ``type`` option of the attribute::
587587

588588
The ``type`` option of ``#[MapRequestPayload]`` was introduced in Symfony 7.1.
589589

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

0 commit comments

Comments
 (0)