Skip to content

Add documentation for #[MapUploadedFile] attribute #19807

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

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
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
93 changes: 93 additions & 0 deletions controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,99 @@ using the ``type`` option of the attribute::

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

.. _controller_map-uploaded-file:

Mapping Uploaded File
~~~~~~~~~~~~~~~~~~~~~

You can map ``UploadedFile``s to the controller arguments and optionally bind ``Constraints`` to them.
The argument resolver fetches the ``UploadedFile`` based on the argument name.

::

namespace App\Controller;

use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\MapUploadedFile;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Validator\Constraints as Assert;

#[Route('/user/picture', methods: ['PUT'])]
class ChangeUserPictureController
{
public function _invoke(
#[MapUploadedFile([
new Assert\File(mimeTypes: ['image/png', 'image/jpeg']),
new Assert\Image(maxWidth: 3840, maxHeight: 2160)
])]
UploadedFile $picture
): Response {
// ...
}
}

.. tip::

The bound ``Constraints`` are performed before injecting the ``UploadedFile`` into the controller argument.
When a constraint violation is detected an ``HTTPException`` is thrown and the controller's
action is not executed.

Mapping ``UploadedFile``s with no custom settings.

.. code-block:: php-attributes

#[MapUploadedFile]
UploadedFile $document

An ``HTTPException`` is thrown when the file is not submitted.
You can skip this check by making the controller argument nullable.

.. code-block:: php-attributes

#[MapUploadedFile]
?UploadedFile $document

.. code-block:: php-attributes

#[MapUploadedFile]
UploadedFile|null $document

``UploadedFile`` collections must be mapped to array or variadic arguments.
The bound ``Constraints`` will be applied to each file in the collection.
If a constraint violation is detected to one of them an ``HTTPException`` is thrown.

.. code-block:: php-attributes

#[MapUploadedFile(new Assert\File(mimeTypes: ['application/pdf']))]
array $documents

.. code-block:: php-attributes

#[MapUploadedFile(new Assert\File(mimeTypes: ['application/pdf']))]
UploadedFile ...$documents

Handling custom names.

.. code-block:: php-attributes

#[MapUploadedFile(name: 'something-else')]
UploadedFile $document

Changing the ``HTTP Status`` thrown when constraint violations are detected.

.. code-block:: php-attributes

#[MapUploadedFile(
constraints: new Assert\File(maxSize: '2M'),
validationFailedStatusCode: Response::HTTP_REQUEST_ENTITY_TOO_LARGE
)]
UploadedFile $document

.. versionadded:: 7.1

The ``#[MapUploadedFile]`` attribute was introduced in Symfony 7.1.

Managing the Session
--------------------

Expand Down
1 change: 1 addition & 0 deletions reference/attributes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ HttpKernel
* :ref:`MapQueryParameter <controller_map-request>`
* :ref:`MapQueryString <controller_map-request>`
* :ref:`MapRequestPayload <controller_map-request>`
* :ref:`MapUploadedFile <controller_map-uploaded-file>`
* :ref:`ValueResolver <managing-value-resolvers>`
* :ref:`WithHttpStatus <framework_exceptions>`
* :ref:`WithLogLevel <framework_exceptions>`
Expand Down
Loading