Skip to content

[HttpKernel] Add support for custom HTTP status code in request mappers #18663

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
Aug 1, 2023
Merged
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
38 changes: 33 additions & 5 deletions controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -431,20 +431,34 @@ attribute in your controller::
}

You can customize the validation groups used during the mapping thanks to the
``validationGroups`` option::
``validationGroups`` option, but also the HTTP status to return if the
validation fails::

use Symfony\Component\HttpFoundation\Response;

// ...

public function dashboard(
#[MapQueryString(validationGroups: ['strict', 'edit'])] UserDTO $userDto
#[MapQueryString(
validationGroups: ['strict', 'edit'],
validationFailedStatusCode: Response::HTTP_UNPROCESSABLE_ENTITY
)] UserDTO $userDto
): Response
{
// ...
}

The default status code returned if the validation fails is 404.

.. versionadded:: 6.3

The :class:`Symfony\\Component\\HttpKernel\\Attribute\\MapQueryString` attribute
was introduced in Symfony 6.3.

.. versionadded:: 6.4

The ``validationFailedStatusCode`` parameter was introduced in Symfony 6.4.

Mapping Request Payload
~~~~~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -492,21 +506,35 @@ your DTO::
// ...
}

You can also customize the validation groups used as well as supported
payload formats::
You can also customize the validation groups used, the status code to return if
the validation fails as well as supported payload formats::

use Symfony\Component\HttpFoundation\Response;

// ...

public function dashboard(
#[MapRequestPayload(acceptFormat: 'json', validationGroups: ['strict', 'read'])] UserDTO $userDto
#[MapRequestPayload(
acceptFormat: 'json',
validationGroups: ['strict', 'read'],
validationFailedStatusCode: Response::HTTP_NOT_FOUND
)] UserDTO $userDto
): Response
{
// ...
}

The default status code returned if the validation fails is 422.

.. versionadded:: 6.3

The :class:`Symfony\\Component\\HttpKernel\\Attribute\\MapRequestPayload` attribute
was introduced in Symfony 6.3.

.. versionadded:: 6.4

The ``validationFailedStatusCode`` parameter was introduced in Symfony 6.4.

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

Expand Down