Skip to content

Commit 57aab65

Browse files
Update controller.rst
1 parent c4bf321 commit 57aab65

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
@@ -543,6 +543,37 @@ if you want to map a nested array of specific DTOs::
543543

544544
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.
545545

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

0 commit comments

Comments
 (0)