diff --git a/validation/sequence_provider.rst b/validation/sequence_provider.rst index 2a06e661034..734a5176c3f 100644 --- a/validation/sequence_provider.rst +++ b/validation/sequence_provider.rst @@ -344,6 +344,100 @@ provides a sequence of groups to be validated: } } +Advanced Validation Group Provider +---------------------------------- + +In the previous section, you learned how to dynamically determine the +sequence of groups to apply based on the state of your entity. This +approach covers most common scenarios, but for more advanced needs, you +may find it to be insufficient. + +For instance, suppose you need to provide the sequence of groups from an +external configuration or service that can change its value dynamically. +Managing the entity initialization and manually setting its dependencies can +be cumbersome, and the implementation might not align with the entity +responsibilities. + +To address this, you can now configure the implementation of the +:class:`Symfony\\Component\\Validator\\GroupProviderInterface` +outside of the entity, and even register the group provider as a +service if necessary. + +Here's how you can achieve this: + + 1) **Define a Separate Group Provider Class:** You can create a class that + implements the :class:`Symfony\\Component\\Validator\\GroupProviderInterface` + and handles the dynamic group sequence logic. + 2) **Configure the User with the Provider:** Use the ``provider`` option within the + :class:`Symfony\\Component\\Validator\\Constraints\\GroupSequenceProvider` + attribute to link the entity with the provider class. + 3) **Autowiring or Manual Tagging:** If autowiring is enabled, your custom provider + will be automatically linked. Otherwise, you can manually tag your service with + ``validator.group_provider``. + +.. configuration-block:: + + .. code-block:: php-attributes + + // src/Entity/User.php + namespace App\Entity; + + // ... + use App\Validator\UserGroupProvider; + + #[Assert\GroupSequenceProvider(provider: UserGroupProvider::class)] + class User + { + // ... + } + + .. code-block:: yaml + + # config/validator/validation.yaml + App\Entity\User: + group_sequence_provider: App\Validator\UserGroupProvider + + .. code-block:: xml + + + + + + + + App\Validator\UserGroupProvider + + + + + + .. code-block:: php + + // src/Entity/User.php + namespace App\Entity; + + // ... + use App\Validator\UserGroupProvider; + use Symfony\Component\Validator\Mapping\ClassMetadata; + + class User + { + // ... + + public static function loadValidatorMetadata(ClassMetadata $metadata): void + { + $metadata->setGroupProvider(UserGroupProvider::class); + $metadata->setGroupSequenceProvider(true); + // ... + } + } + +With this approach, you can maintain a clean separation between the entity +structure and the group sequence logic, allowing for more advanced use cases. + How to Sequentially Apply Constraints on a Single Property ---------------------------------------------------------- pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy