Skip to content

Commit 0408c0a

Browse files
Add MapRequestPayload tips for Enum
1 parent 7c5c13f commit 0408c0a

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

controller.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,37 @@ if you want to map a nested array of specific DTOs::
539539
) {}
540540
}
541541

542+
.. tip::
543+
544+
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.
545+
Validating an Enum in your `#[MapRequestPayload]` class should look like this::
546+
547+
class LuckyController
548+
{
549+
#[Route('/lucky/number/{max}', name: 'app_lucky_number', methods: ['POST'])]
550+
public function number(#[MapRequestPayload] MyInput $input, int $max): Response
551+
{
552+
// use it like this : $input->myInputAttribute;
553+
}
554+
}
555+
556+
class MyInput
557+
{
558+
#[Assert\Choice(callback: [MyEnum::class, 'values'])]
559+
public string $myInputAttribute;
560+
}
561+
562+
enum MyEnum: string
563+
{
564+
case FIRST_CASE = 'first_case';
565+
case SECOND_CASE = 'second_case';
566+
567+
public static function values(): array
568+
{
569+
return array_column(self::cases(), 'value');
570+
}
571+
}
572+
542573
Managing the Session
543574
--------------------
544575

0 commit comments

Comments
 (0)