8000 feature #51348 [FrameworkBundle][Validator] Allow implementing valida… · symfony/symfony@0e201d1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0e201d1

Browse files
committed
feature #51348 [FrameworkBundle][Validator] Allow implementing validation groups provider outside DTOs (Yonel Ceruto)
This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [FrameworkBundle][Validator] Allow implementing validation groups provider outside DTOs | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | symfony/symfony-docs#18744 Alternative to #51233 Inspiration: #51012 Currently, you can determine the sequence of groups to apply dynamically based on the state of your DTO by implementing the `GroupSequenceProviderInterface` in your DTO class. https://symfony.com/doc/current/validation/sequence_provider.html#group-sequence-providers ```php use Symfony\Component\Validator\GroupSequenceProviderInterface; #[Assert\GroupSequenceProvider] class UserDto implements GroupSequenceProviderInterface { // ... public function getGroupSequence(): array|GroupSequence { if ($this->isCompanyType()) { return ['User', 'Company']; } return ['User']; } } ``` It covers most of the common scenarios, but for more advanced ones, it may not be sufficient. Suppose now you need to provide the sequence of groups from an external configuration (or service) which can change its value dynamically: ```php #[Assert\GroupSequenceProvider] class UserDto implements GroupSequenceProviderInterface { // ... public __constructor(private readonly ConfigService $config) { } public function getGroupSequence(): array|GroupSequence { if ($this->config->isEnabled()) { return ['User', $this->config->getGroup()]; } return ['User']; } } ``` This issue cannot be resolved at present without managing the DTO initialization and manually setting its dependencies. On the other hand, since the state of the DTO is not used at all, the implementation of the `GroupSequenceProviderInterface` becomes less fitting to the DTO responsibility. Further, stricter programming may raise a complaint about a violation of SOLID principles here. So, the proposal of this PR is to allow configuring the validation groups provider outside of the DTO, while simultaneously enabling the registration of this provider as a service if necessary. To achieve this, you'll need to implement a new `GroupProviderInterface` in a separate class, and configure it using the new `provider` option within the `GroupSequenceProvider` attribute: ```php #[Assert\GroupSequenceProvider(provider: UserGroupProvider::class)] class UserDto { // ... } class UserGroupProvider implements GroupProviderInterface { public __constructor(private readonly ConfigService $config) { } public function getGroups(object $object): array|GroupSequence { if ($this->config->isEnabled()) { return ['User', $this->config->getGroup()]; } return ['User']; } } ``` That's all you'll need to do if autowiring is enabled under your custom provider. Otherwise, you can manually tag your service with `validator.group_provider` to collect it and utilize it as a provider service during the validation process. In conclusion, no more messing with the DTO structure, just use the new `class` option for more advanced use cases. --- TODO: - [x] Add tests - [x] Create doc PR Commits ------- a3a089a [FrameworkBundle][Validator] Allow implementing validation groups provider outside DTOs
2 parents a19c534 + a3a089a commit 0e201d1

24 files changed

+316
-72
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/UnusedTagsPass.php

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ class UnusedTagsPass implements CompilerPassInterface
101101
'twig.runtime',
102102
'validator.auto_mapper',
103103
'validator.constraint_validator',
104+
'validator.group_provider',
104105
'validator.initializer',
105106
'workflow',
106107
];

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

+3
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@
177177
use Symfony\Component\Uid\UuidV4;
178178
use Symfony\Component\Validator\Constraints\ExpressionLanguageProvider;
179179
use Symfony\Component\Validator\ConstraintValidatorInterface;
180+
use Symfony\Component\Validator\GroupProviderInterface;
180181
use Symfony\Component\Validator\Mapping\Loader\PropertyInfoLoader;
181182
use Symfony\Component\Validator\ObjectInitializerInterface;
182183
use Symfony\Component\Validator\Validation;
@@ -657,6 +658,8 @@ public function load(array $configs, ContainerBuilder $container)
657658
->addTag('serializer.normalizer');
658659
$container->registerForAutoconfiguration(ConstraintValidatorInterface::class)
659660
->addTag('validator.constraint_validator');
661+
$container->registerForAutoconfiguration(GroupProviderInterface::class)
662+
->addTag('validator.group_provider');
660663
$container->registerForAutoconfiguration(ObjectInitializerInterface::class)
661664
->addTag('validator.initializer');
662665
$container->registerForAutoconfiguration(MessageHandlerInterface::class)

src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.php

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
->call('setConstraintValidatorFactory', [
4343
service('validator.validator_factory'),
4444
])
45+
->call('setGroupProviderLocator', [
46+< 10000 div class="diff-text-inner"> tagged_locator('validator.group_provider'),
47+
])
4548
->call('setTranslator', [
4649
service('translator')->ignoreOnInvalid(),
4750
])

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTestCase.php

+44-42
Original file line numberDiff line numberDiff line change
@@ -1230,16 +1230,18 @@ public function testValidation()
12301230

12311231
$annotations = !class_exists(FullStack::class);
12321232

1233-
$this->assertCount($annotations ? 7 : 6, $calls);
1233+
$this->assertCount($annotations ? 8 : 7, $calls);
12341234
$this->assertSame('setConstraintValidatorFactory', $calls[0][0]);
12351235
$this->assertEquals([new Reference('validator.validator_factory')], $calls[0][1]);
1236-
$this->assertSame('setTranslator', $calls[1][0]);
1237-
$this->assertEquals([new Reference('translator', ContainerBuilder::IGNORE_ON_INVALID_REFERENCE)], $calls[1][1]);
1238-
$this->assertSame('setTranslationDomain', $calls[2][0]);
1239-
$this->assertSame(['%validator.translation_domain%'], $calls[2][1]);
1240-
$this->assertSame('addXmlMappings', $calls[3][0]);
1241-
$this->assertSame([$xmlMappings], $calls[3][1]);
1242-
$i = 3;
1236+
$this->assertSame('setGroupProviderLocator', $calls[1][0]);
1237+
$this->assertInstanceOf(ServiceLocatorArgument::class, $calls[1][1][0]);
1238+
$this->assertSame('setTranslator', $calls[2][0]);
1239+
$this->assertEquals([new Reference('translator', ContainerBuilder::IGNORE_ON_INVALID_REFERENCE)], $calls[2][1]);
1240+
$this->assertSame('setTranslationDomain', $calls[3][0]);
1241+
$this->assertSame(['%validator.translation_domain%'], $calls[3][1]);
1242+
$this->assertSame('addXmlMappings', $calls[4][0]);
1243+
$this->assertSame([$xmlMappings], $calls[4][1]);
1244+
$i = 4;
12431245
if ($annotations) {
12441246
$this->assertSame('enableAttributeMapping', $calls[++$i][0]);
12451247
}
@@ -1288,12 +1290,12 @@ public function testValidationAttributes()
12881290

12891291
$calls = $container->getDefinition('validator.builder')->getMethodCalls();
12901292

1291-
$this->assertCount(7, $calls);
1292-
$this->assertSame('enableAttributeMapping', $calls[4][0]);
1293-
$this->assertSame('addMethodMapping', $calls[5][0]);
1294-
$this->assertSame(['loadValidatorMetadata'], $calls[5][1]);
1295-
$this->assertSame('setMappingCache', $calls[6][0]);
1296-
$this->assertEquals([new Reference('validator.mapping.cache.adapter')], $calls[6][1]);
1293+
$this->assertCount(8, $calls);
1294+
$this->assertSame('enableAttributeMapping', $calls[5][0]);
1295+
$this->assertSame('addMethodMapping', $calls[6][0]);
1296+
$this->assertSame(['loadValidatorMetadata'], $calls[6][1]);
1297+
$this->assertSame('setMappingCache', $calls[7][0]);
1298+
$this->assertEquals([new Reference('validator.mapping.cache.adapter')], $calls[7][1]);
12971299
// no cache this time
12981300
}
12991301

@@ -1308,14 +1310,14 @@ public function testValidationLegacyAnnotations()
13081310

13091311
$calls = $container->getDefinition('validator.builder')->getMethodCalls();
13101312

1311-
$this->assertCount(8, $calls);
1312-
$this->assertSame('enableAttributeMapping', $calls[4][0]);
1313+
$this->assertCount(9, $calls);
1314+
$this->assertSame('enableAttributeMapping', $calls[5][0]);
13131315
if (method_exists(ValidatorBuilder::class, 'setDoctrineAnnotationReader')) {
1314-
$this->assertSame('setDoctrineAnnotationReader', $calls[5][0]);
1315-
$this->assertEquals([new Reference('annotation_reader')], $calls[5][1]);
1316-
$i = 6;
1316+
$this->assertSame('setDoctrineAnnotationReader', $calls[6][0]);
1317+
$this->assertEquals([new Reference('annotation_reader')], $calls[6][1]);
1318+
$i = 7;
13171319
} else {
1318-
$i = 5;
1320+
$i = 6;
13191321
}
13201322
$this->assertSame('addMethodMapping', $calls[$i][0]);
13211323
$this->assertSame(['loadValidatorMetadata'], $calls[$i][1]);
@@ -1335,16 +1337,16 @@ public function testValidationPaths()
13351337

13361338
$calls = $container->getDefinition('validator.builder')->getMethodCalls();
13371339

1338-
$this->assertCount(8, $calls);
1339-
$this->assertSame('addXmlMappings', $calls[3][0]);
1340-
$this->assertSame('addYamlMappings', $calls[4][0]);
1341-
$this->assertSame('enableAttributeMapping', $calls[5][0]);
1342-
$this->assertSame('addMethodMapping', $calls[6][0]);
1343-
$this->assertSame(['loadValidatorMetadata'], $calls[6][1]);
1344-
$this->assertSame('setMappingCache', $calls[7][0]);
1345-
$this->assertEquals([new Reference('validator.mapping.cache.adapter')], $calls[7][1]);
1340+
$this->assertCount(9, $calls);
1341+
$this->assertSame('addXmlMappings', $calls[4][0]);
1342+
$this->assertSame('addYamlMappings', $calls[5][0]);
1343+
$this->assertSame('enableAttributeMapping', $calls[6][0]);
1344+
$this->assertSame('addMethodMapping', $calls[7][0]);
1345+
$this->assertSame(['loadValidatorMetadata'], $calls[7][1]);
1346+
$this->assertSame('setMappingCache', $calls[8][0]);
1347+
$this->assertEquals([new Reference('validator.mapping.cache.adapter')], $calls[8][1]);
13461348

1347-
$xmlMappings = $calls[3][1][0];
1349+
$xmlMappings = $calls[4][1][0];
13481350
$this->assertCount(3, $xmlMappings);
13491351
try {
13501352
// Testing symfony/symfony
@@ -1355,7 +1357,7 @@ public function testValidationPaths()
13551357
}
13561358
$this->assertStringEndsWith('TestBundle/Resources/config/validation.xml', $xmlMappings[1]);
13571359

1358-
$yamlMappings = $calls[4][1][0];
1360+
$yamlMappings = $calls[5][1][0];
13591361
$this->assertCount(1, $yamlMappings);
13601362
$this->assertStringEndsWith('TestBundle/Resources/config/validation.yml', $yamlMappings[0]);
13611363
}
@@ -1370,7 +1372,7 @@ public function testValidationPathsUsingCustomBundlePath()
13701372
]);
13711373

13721374
$calls = $container->getDefinition('validator.builder')->getMethodCalls();
1373-
$xmlMappings = $calls[3][1][0];
1375+
$xmlMappings = $calls[4][1][0];
13741376
$this->assertCount(3, $xmlMappings);
13751377

13761378
try {
@@ -1382,7 +1384,7 @@ public function testValidationPathsUsingCustomBundlePath()
13821384
}
13831385
$this->assertStringEndsWith('CustomPathBundle/Resources/config/validation.xml', $xmlMappings[1]);
13841386

1385-
$yamlMappings = $calls[4][1][0];
1387+
$yamlMappings = $calls[5][1][0];
13861388
$this->assertCount(1, $yamlMappings);
13871389
$this->assertStringEndsWith('CustomPathBundle/Resources/config/validation.yml', $yamlMappings[0]);
13881390
}
@@ -1395,9 +1397,9 @@ public function testValidationNoStaticMethod()
13951397

13961398
$annotations = !class_exists(FullStack::class);
13971399

1398-
$this->assertCount($annotations ? 6 : 5, $calls);
1399-
$this->assertSame('addXmlMappings', $calls[3][0]);
1400-
$i = 3;
1400+
$this->assertCount($annotations ? 7 : 6, $calls);
1401+
$this->assertSame('addXmlMappings', $calls[4][0]);
1402+
$i = 4;
14011403
if ($annotations) {
14021404
$this->assertSame('enableAttributeMapping', $calls[++$i][0]);
14031405
}
@@ -1426,14 +1428,14 @@ public function testValidationMapping()
14261428

14271429
$calls = $container->getDefinition('validator.builder')->getMethodCalls();
14281430

1429-
$this->assertSame('addXmlMappings', $calls[3][0]);
1430-
$this->assertCount(3, $calls[3][1][0]);
1431-
1432-
$this->assertSame('addYamlMappings', $calls[4][0]);
1431+
$this->assertSame('addXmlMappings', $calls[4][0]);
14331432
$this->assertCount(3, $calls[4][1][0]);
1434-
$this->assertStringContainsString('foo.yml', $calls[4][1][0][0]);
1435-
$this->assertStringContainsString('validation.yml', $calls[4][1][0][1]);
1436-
$this->assertStringContainsString('validation.yaml', $calls[4][1][0][2]);
1433+
1434+
$this->assertSame('addYamlMappings', $calls[5][0]);
1435+
$this->assertCount(3, $calls[5][1][0]);
1436+
$this->assertStringContainsString('foo.yml', $calls[5][1][0][0]);
1437+
$this->assertStringContainsString('validation.yml', $calls[5][1][0][1]);
1438+
$this->assertStringContainsString('validation.yaml', $calls[5][1][0][2]);
14371439
}
14381440

14391441
public function testValidationAutoMapping()

src/Symfony/Component/Validator/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ CHANGELOG
1515
* Deprecate `ValidatorBuilder::enableAnnotationMapping()`, use `ValidatorBuilder::enableAttributeMapping()` instead
1616
* Deprecate `ValidatorBuilder::disableAnnotationMapping()`, use `ValidatorBuilder::disableAttributeMapping()` instead
1717
* Deprecate `AnnotationLoader`, use `AttributeLoader` instead
18+
* Add `GroupProviderInterface` to implement validation group providers outside the underlying class
1819

1920
6.3
2021
---

src/Symfony/Component/Validator/Constraints/GroupSequenceProvider.php

+9
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,25 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14+
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
15+
use Symfony\Component\Validator\Attribute\HasNamedArguments;
16+
1417
/**
1518
* Attribute to define a group sequence provider.
1619
*
1720
* @Annotation
1821
*
22+
* @NamedArgumentConstructor
23+
*
1924
* @Target({"CLASS", "ANNOTATION"})
2025
*
2126
* @author Bernhard Schussek <bschussek@gmail.com>
2227
*/
2328
#[\Attribute(\Attribute::TARGET_CLASS)]
2429
class GroupSequenceProvider
2530
{
31+
#[HasNamedArguments]
32+
public function __construct(public ?string $provider = null)
33+
{
34+
}
2635
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator;
13+
14+
use Symfony\Component\Validator\Constraints\GroupSequence;
15+
16+
/**
17+
* Defines the interface for a validation group provider.
18+
*
19+
* @author Yonel Ceruto <yonelceruto@gmail.com>
20+
*/
21+
interface GroupProviderInterface
22+
{
23+
/**
24+
* Returns which validation groups should be used for a certain state
25+
* of the object.
26+
*
27+
* @return string[]|string[][]|GroupSequence
28+
*/
29+
public function getGroups(object $object): array|GroupSequence;
30+
}

src/Symfony/Component/Validator/Mapping/ClassMetadata.php

+20-1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ class ClassMetadata extends GenericMetadata implements ClassMetadataInterface
8686
*/
8787
public bool $groupSequenceProvider = false;
8888

89+
/**
90+
* @internal This property is public in order to reduce the size of the
91+
* class' serialized representation. Do not access it. Use
92+
* {@link getGroupProvider()} instead.
93+
*/
94+
public ?string $groupProvider = null;
95+
8996
/**
9097
* The strategy for traversing traversable objects.
9198
*
@@ -123,6 +130,7 @@ public function __sleep(): array
123130
'getters',
124131
'groupSequence',
125132
'groupSequenceProvider',
133+
'groupProvider',
126134
'members',
127135
'name',
128136
'properties',
@@ -319,6 +327,7 @@ public function addGetterMethodConstraints(string $property, string $method, arr
319327
public function mergeConstraints(self $source)
320328
{
321329
if ($source->isGroupSequenceProvider()) {
330+
$this->setGroupProvider($source->getGroupProvider());
322331
$this->setGroupSequenceProvider(true);
323332
}
324333

@@ -432,7 +441,7 @@ public function setGroupSequenceProvider(bool $active)
432441
throw new GroupDefinitionException('Defining a group sequence provider is not allowed with a static group sequence.');
433442
}
434443

435-
if (!$this->getReflectionClass()->implementsInterface(GroupSequenceProviderInterface::class)) {
444+
if (null === $this->groupProvider && !$this->getReflectionClass()->implementsInterface(GroupSequenceProviderInterface::class)) {
436445
throw new GroupDefinitionException(sprintf('Class "%s" must implement GroupSequenceProviderInterface.', $this->name));
437446
}
438447

@@ -444,6 +453,16 @@ public function isGroupSequenceProvider(): bool
444453
return $this->groupSequenceProvider;
445454
}
446455

456+
public function setGroupProvider(?string $provider): void
457+
{
458+
$this->groupProvider = $provider;
459+
}
460+
461+
public function getGroupProvider(): ?string
462+
{
463+
return $this->groupProvider;
464+
}
465+
447466
public function getCascadingStrategy(): int
448467
{
449468
return $this->cascadingStrategy;

src/Symfony/Component/Validator/Mapping/ClassMetadataInterface.php

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
* @see GroupSequence
3131
* @see GroupSequenceProviderInterface
3232
* @see TraversalStrategy
33+
*
34+
* @method string|null getGroupProvider()
3335
*/
3436
interface ClassMetadataInterface extends MetadataInterface
3537
{

src/Symfony/Component/Validator/Mapping/Loader/AnnotationLoader.php

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public function loadClassMetadata(ClassMetadata $metadata): bool
5151
if ($constraint instanceof GroupSequence) {
5252
$metadata->setGroupSequence($constraint->groups);
5353
} elseif ($constraint instanceof GroupSequenceProvider) {
54+
$metadata->setGroupProvider($constraint->provider);
5455
$metadata->setGroupSequenceProvider(true);
5556
} elseif ($constraint instanceof Constraint) {
5657
$metadata->addConstraint($constraint);

src/Symfony/Component/Validator/Mapping/Loader/XmlFileLoader.php

+1
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ private function loadClassesFromXml(): void
201201
private function loadClassMetadataFromXml(ClassMetadata $metadata, \SimpleXMLElement $classDescription): void
202202
{
203203
if (\count($classDescription->{'group-sequence-provider'}) > 0) {
204+
$metadata->setGroupProvider($classDescription->{'group-sequence-provider'}[0]->value ?: null);
204205
$metadata->setGroupSequenceProvider(true);
205206
}
206207

src/Symfony/Component/Validator/Mapping/Loader/YamlFileLoader.php

+3
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ private function loadClassesFromYaml(): void
150150
private function loadClassMetadataFromYaml(ClassMetadata $metadata, array $classDescription): void
151151
{
152152
if (isset($classDescription['group_sequence_provider'])) {
153+
if (\is_string($classDescription['group_sequence_provider'])) {
154+
$metadata->setGroupProvider($classDescription['group_sequence_provider']);
155+
}
153156
$metadata->setGroupSequenceProvider(
154157
(bool) $classDescription['group_sequence_provider']
155158
);

0 commit comments

Comments
 (0)
0