@@ -1306,6 +1306,85 @@ In Twig templates, metadata is available via the ``workflow_metadata()`` functio
13061306 </ul>
13071307 </p>
13081308
1309+ Adding Custom Definition Validators
1310+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1311+
1312+ Sometimes, you may want to add custom logics to validate your workflow definition.
1313+ To do this, you need to implement the
1314+ :class: `Symfony\\ Component\\ Workflow\\ Validator\\ DefinitionValidatorInterface `::
1315+
1316+ namespace App\Workflow\Validator;
1317+
1318+ use Symfony\Component\Workflow\Definition;
1319+ use Symfony\Component\Workflow\Exception\InvalidDefinitionException;
1320+ use Symfony\Component\Workflow\Validator\DefinitionValidatorInterface;
1321+
1322+ final class BlogPublishingValidator implements DefinitionValidatorInterface
1323+ {
1324+ public function validate(Definition $definition, string $name): void
1325+ {
1326+ if (!$definition->getMetadataStore()->getMetadata('title')) {
1327+ throw new InvalidDefinitionException(sprintf('The workflow metadata title is missing in Workflow "%s".', $name));
1328+ }
1329+ }
1330+ }
1331+
1332+ Once your definition validator is implemented, you can configure your workflow to use
1333+ it:
1334+
1335+ .. configuration-block ::
1336+
1337+ .. code-block :: yaml
1338+
1339+ # config/packages/workflow.yaml
1340+ framework :
1341+ workflows :
1342+ blog_publishing :
1343+ # ... previous configuration
1344+
1345+ definition_validators :
1346+ - App\Workflow\Validator\BlogPublishingValidator
1347+
1348+ .. code-block :: xml
1349+
1350+ <!-- config/packages/workflow.xml -->
1351+ <?xml version =" 1.0" encoding =" UTF-8" ?>
1352+ <container xmlns =" http://symfony.com/schema/dic/services"
1353+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
1354+ xmlns : framework =" http://symfony.com/schema/dic/symfony"
1355+ xsi : schemaLocation =" http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
1356+ http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd"
1357+ >
1358+ <framework : config >
1359+ <framework : workflow name =" blog_publishing" >
1360+ <!-- ... previous configuration -->
1361+ <framework : definition-validators >App\Workflow\Validator\BlogPublishingValidator</framework : definition-validators >
1362+ </framework : workflow >
1363+ </framework : config >
1364+ </container >
1365+
1366+ .. code-block :: php
1367+
1368+ // config/packages/workflow.php
1369+ use Symfony\Config\FrameworkConfig;
1370+
1371+ return static function (FrameworkConfig $framework): void {
1372+ $blogPublishing = $framework->workflows()->workflows('blog_publishing');
1373+ // ... previous configuration
1374+
1375+ $blogPublishing->definitionValidators([
1376+ App\Workflow\Validator\BlogPublishingValidator::class
1377+ ]);
1378+
1379+ // ...
1380+ };
1381+
1382+ The ``BlogPublishingValidator `` definition validator will be executed during the container compilation.
1383+
1384+ .. versionadded :: 7.3
1385+
1386+ Support for defining custom workflow definition validators was introduced in Symfony 7.3.
1387+
13091388Learn more
13101389----------
13111390
0 commit comments