Skip to content

Commit 630cac8

Browse files
committed
minor #19807 Add documentation for #[MapUploadedFile] attribute (renedelima)
This PR was merged into the 7.1 branch. Discussion ---------- Add documentation for `#[MapUploadedFile]` attribute Closes #19802 Commits ------- 4335516 Add documentation for #[MapUploadedFile] attribute
2 parents 7522724 + 4335516 commit 630cac8

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

controller.rst

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
590683
Managing the Session
591684
--------------------
592685

reference/attributes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ HttpKernel
6464
* :ref:`MapQueryParameter <controller_map-request>`
6565
* :ref:`MapQueryString <controller_map-request>`
6666
* :ref:`MapRequestPayload <controller_map-request>`
67+
* :ref:`MapUploadedFile <controller_map-uploaded-file>`
6768
* :ref:`ValueResolver <managing-value-resolvers>`
6869
* :ref:`WithHttpStatus <framework_exceptions>`
6970
* :ref:`WithLogLevel <framework_exceptions>`

0 commit comments

Comments
 (0)