From e0fcd1c3bd46b7bca6bd4a1eeb7e3c687d0d3cf6 Mon Sep 17 00:00:00 2001 From: Romain Monteil Date: Thu, 15 May 2025 15:20:18 +0200 Subject: [PATCH] [Workflow] Document custom workflow definition validator --- workflow.rst | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/workflow.rst b/workflow.rst index 11b4005bb10..544f775cce0 100644 --- a/workflow.rst +++ b/workflow.rst @@ -1306,6 +1306,85 @@ In Twig templates, metadata is available via the ``workflow_metadata()`` functio

+Adding Custom Definition Validators +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Sometimes, you may want to add custom logics to validate your workflow definition. +To do this, you need to implement the +:class:`Symfony\\Component\\Workflow\\Validator\\DefinitionValidatorInterface`:: + + namespace App\Workflow\Validator; + + use Symfony\Component\Workflow\Definition; + use Symfony\Component\Workflow\Exception\InvalidDefinitionException; + use Symfony\Component\Workflow\Validator\DefinitionValidatorInterface; + + final class BlogPublishingValidator implements DefinitionValidatorInterface + { + public function validate(Definition $definition, string $name): void + { + if (!$definition->getMetadataStore()->getMetadata('title')) { + throw new InvalidDefinitionException(sprintf('The workflow metadata title is missing in Workflow "%s".', $name)); + } + } + } + +Once your definition validator is implemented, you can configure your workflow to use +it: + +.. configuration-block:: + + .. code-block:: yaml + + # config/packages/workflow.yaml + framework: + workflows: + blog_publishing: + # ... previous configuration + + definition_validators: + - App\Workflow\Validator\BlogPublishingValidator + + .. code-block:: xml + + + + + + + + App\Workflow\Validator\BlogPublishingValidator + + + + + .. code-block:: php + + // config/packages/workflow.php + use Symfony\Config\FrameworkConfig; + + return static function (FrameworkConfig $framework): void { + $blogPublishing = $framework->workflows()->workflows('blog_publishing'); + // ... previous configuration + + $blogPublishing->definitionValidators([ + App\Workflow\Validator\BlogPublishingValidator::class + ]); + + // ... + }; + +The ``BlogPublishingValidator`` definition validator will be executed during the container compilation. + +.. versionadded:: 7.3 + + Support for defining custom workflow definition validators was introduced in Symfony 7.3. + Learn more ----------