From a12e92d44594c0626822a734cfd11d7e09adc4bb Mon Sep 17 00:00:00 2001 From: Kai Dederichs Date: Sat, 13 Aug 2022 21:31:38 +0200 Subject: [PATCH] Add section on how to handle group sequence providers --- core/validation.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/core/validation.md b/core/validation.md index 73c9301b5ee..b2f81585df0 100644 --- a/core/validation.md +++ b/core/validation.md @@ -377,6 +377,43 @@ class Greeting } ``` +## Using the Group Sequence Provider + +If you need to use a Group Sequence Provider, you'll have to tell the Serializer to ignore the getter added by the Interface: + +```php +class Greeting implements GroupSequenceProvider +{ + #[ORM\Id, ORM\Column, ORM\GeneratedValue] + private ?int $id = null; + + #[ORM\Column] + #[Assert\NotBlank(groups: ['needs_name']) + public string $name = ''; + + public function getId(): int + { + return $this->id; + } + + // You need to add this Ignore statement. + #[Ignore] + public function getGroupSequence(): array|GroupSequence + { + $groups = [ + 'Greeting', + ]; + + if ($this->getId() % 2) { + $groups[] = 'needs_name'; + } + + return new GroupSequence($groups); + } +} +``` + + ## Validating Delete Operations By default, validation rules that are specified on the API resource are not evaluated during DELETE operations. You need to trigger the validation in your code, if needed.