Skip to content

Commit ab0326d

Browse files
[HttpKernel] Add support for custom HTTP status code in request mappers
1 parent 8e87d62 commit ab0326d

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

controller.rst

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -431,20 +431,34 @@ attribute in your controller::
431431
}
432432

433433
You can customize the validation groups used during the mapping thanks to the
434-
``validationGroups`` option::
434+
``validationGroups`` option, but also the HTTP status to return if the
435+
validation fails::
436+
437+
use Symfony\Component\HttpFoundation\Response;
438+
439+
// ...
435440

436441
public function dashboard(
437-
#[MapQueryString(validationGroups: ['strict', 'edit'])] UserDTO $userDto
442+
#[MapQueryString(
443+
validationGroups: ['strict', 'edit'],
444+
validationFailedStatusCode: Response::HTTP_UNPROCESSABLE_ENTITY
445+
)] UserDTO $userDto
438446
): Response
439447
{
440448
// ...
441449
}
442450

451+
The default status code returned if the validation fails is 404.
452+
443453
.. versionadded:: 6.3
444454

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

458+
.. versionadded:: 6.4
459+
460+
The ``validationFailedStatusCode`` parameter was introduced in Symfony 6.4.
461+
448462
Mapping Request Payload
449463
~~~~~~~~~~~~~~~~~~~~~~~
450464

@@ -492,21 +506,35 @@ your DTO::
492506
// ...
493507
}
494508

495-
You can also customize the validation groups used as well as supported
496-
payload formats::
509+
You can also customize the validation groups used, the status code to return if
510+
the validation fails as well as supported payload formats::
511+
512+
use Symfony\Component\HttpFoundation\Response;
513+
514+
// ...
497515

498516
public function dashboard(
499-
#[MapRequestPayload(acceptFormat: 'json', validationGroups: ['strict', 'read'])] UserDTO $userDto
517+
#[MapRequestPayload(
518+
acceptFormat: 'json',
519+
validationGroups: ['strict', 'read'],
520+
validationFailedStatusCode: Response::HTTP_NOT_FOUND
521+
)] UserDTO $userDto
500522
): Response
501523
{
502524
// ...
503525
}
504526

527+
The default status code returned if the validation fails is 422.
528+
505529
.. versionadded:: 6.3
506530

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

534+
.. versionadded:: 6.4
535+
536+
The ``validationFailedStatusCode`` parameter was introduced in Symfony 6.4.
537+
510538
Managing the Session
511539
--------------------
512540

0 commit comments

Comments
 (0)