10000 Add MapRequestPayload tips with built-in types by AurelienPillevesse · Pull Request #19590 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

Add MapRequestPayload tips with built-in types #19590

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

Open
wants to merge 3 commits into
base: 7.1
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
Add MapRequestPayload tips for Enum
  • Loading branch information
AurelienPillevesse committed Feb 27, 2024
commit 0408c0a4df90155d8fc2037033d27f6b54de70e1
31 changes: 31 additions & 0 deletions controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,37 @@ if you want to map a nested array of specific DTOs::
) {}
}

.. tip::

If using typed properties with `MapRequestPayload`, it's recommanded to use built-in types (like `int`, `bool`, `string`...) for mapping. Using custom types can expose your application implementation outside with errors during denormalization.
Validating an Enum in your `#[MapRequestPayload]` class should look like this::

class LuckyController
{
#[Route('/lucky/number/{max}', name: 'app_lucky_number', methods: ['POST'])]
public function number(#[MapRequestPayload] MyInput $input, int $max): Response
{
// use it like this : $input->myInputAttribute;
}
}

class MyInput
{
#[Assert\Choice(callback: [MyEnum::class, 'values'])]
public string $myInputAttribute;
}

enum MyEnum: string
{
case FIRST_CASE = 'first_case';
case SECOND_CASE = 'second_case';

public static function values(): array
{
return array_column(self::cases(), 'value');
}
}

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

Expand Down
0