Skip to content

Commit 6b27438

Browse files
committed
minor #19627 [HttpKernel] Explain how to define default value in MapQueryString (NeilPeyssard)
This PR was merged into the 6.4 branch. Discussion ---------- [HttpKernel] Explain how to define default value in MapQueryString Hello, The current example of how using a `MapQueryString` attribute is not really clear about default DTO value. Some people expects to have a new instance of their DTO. The goal of this PR is to add a bit of details about how to define this default value. see [#54042](symfony/symfony#54042) Commits ------- 55616d8 [HttpKernel] Explain how to define default value in MapQueryString
2 parents 58d9f73 + 55616d8 commit 6b27438

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

controller.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,24 @@ HTTP status to return if the validation fails::
451451

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

454+
If the request query string is empty and if you still need to have a valid DTO, you can
455+
define a default value in your controller method::
456+
457+
use App\Model\UserDto;
458+
use Symfony\Component\HttpFoundation\Response;
459+
use Symfony\Component\HttpKernel\Attribute\MapQueryString;
460+
461+
// ...
462+
463+
public function dashboard(
464+
#[MapQueryString] UserDTO $userDto = new UserDTO()
465+
): Response
466+
{
467+
// ...
468+
}
469+
470+
In this case your DTO should have default values.
471+
454472
.. versionadded:: 6.3
455473

456474
The :class:`Symfony\\Component\\HttpKernel\\Attribute\\MapQueryString` attribute

0 commit comments

Comments
 (0)