@@ -431,20 +431,34 @@ attribute in your controller::
431
431
}
432
432
433
433
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
+ // ...
435
440
436
441
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
438
446
): Response
439
447
{
440
448
// ...
441
449
}
442
450
451
+ The default status code returned if the validation fails is 404.
452
+
443
453
.. versionadded :: 6.3
444
454
445
455
The :class: `Symfony\\ Component\\ HttpKernel\\ Attribute\\ MapQueryString ` attribute
446
456
was introduced in Symfony 6.3.
447
457
458
+ .. versionadded :: 6.4
459
+
460
+ The ``validationFailedStatusCode `` parameter was introduced in Symfony 6.4.
461
+
448
462
Mapping Request Payload
449
463
~~~~~~~~~~~~~~~~~~~~~~~
450
464
@@ -492,21 +506,35 @@ your DTO::
492
506
// ...
493
507
}
494
508
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
+ // ...
497
515
498
516
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
500
522
): Response
501
523
{
502
524
// ...
503
525
}
504
526
527
+ The default status code returned if the validation fails is 422.
528
+
505
529
.. versionadded :: 6.3
506
530
507
531
The :class: `Symfony\\ Component\\ HttpKernel\\ Attribute\\ MapRequestPayload ` attribute
508
532
was introduced in Symfony 6.3.
509
533
534
+ .. versionadded :: 6.4
535
+
536
+ The ``validationFailedStatusCode `` parameter was introduced in Symfony 6.4.
537
+
510
538
Managing the Session
511
539
--------------------
512
540
0 commit comments