Skip to content

[HttpKernel] Explain how to define default value in MapQueryString #19627

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
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
18 changes: 18 additions & 0 deletions controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,24 @@ HTTP status to return if the validation fails::

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

If the request query string is empty and if you still need to have a valid DTO, you can
define a default value in your controller method::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
define a default value in your controller method::
define a default value to your controller parameter::

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tweaked this while merging ... but apparently, the correct preposition here is for instead of to 😊 Thanks!


use App\Model\UserDto;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\MapQueryString;

// ...

public function dashboard(
#[MapQueryString] UserDTO $userDto = new UserDTO()
): Response
{
// ...
}

In this case your DTO should have default values.

Comment on lines +470 to +471
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line may be superfluous 🙂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I removed it while merging. Thanks.

.. versionadded:: 6.3

The :class:`Symfony\\Component\\HttpKernel\\Attribute\\MapQueryString` attribute
Expand Down