Skip to content

Commit

Permalink
bug #33828 [DoctrineBridge] Auto-validation must work if no regex are…
Browse files Browse the repository at this point in the history
… passed (dunglas)

This PR was squashed before being merged into the 4.3 branch (closes #33828).

Discussion
----------

[DoctrineBridge] Auto-validation must work if no regex are passed

| Q             | A
| ------------- | ---
| Branch?       | 4.3
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | n/a
| License       | MIT
| Doc PR        | n/a

Backport of https://github.com/symfony/symfony/pull/32107/files#r295762928.
This behavior if faulty, if no regex are passed, autvalidation must be triggered, [as done in `PropertyInfoLoader`](https://github.com/symfony/symfony/blob/4.3/src/Symfony/Component/Validator/Mapping/Loader/PropertyInfoLoader.php#L50).

Commits
-------

5ed7d6c [DoctrineBridge] Auto-validation must work if no regex are passed
  • Loading branch information
dunglas committed Oct 29, 2019
2 parents f233259 + 5ed7d6c commit ee4b99f
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 3 deletions.
Expand Up @@ -173,7 +173,7 @@ public function testClassValidator(bool $expected, string $classValidatorRegexp
public function regexpProvider()
{
return [
[false, null],
[true, null],
[true, '{^'.preg_quote(DoctrineLoaderEntity::class).'$|^'.preg_quote(Entity::class).'$}'],
[false, '{^'.preg_quote(Entity::class).'$}'],
];
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Doctrine/Validator/DoctrineLoader.php
Expand Up @@ -43,7 +43,7 @@ public function __construct(EntityManagerInterface $entityManager, string $class
public function loadClassMetadata(ClassMetadata $metadata): bool
{
$className = $metadata->getClassName();
if (null === $this->classValidatorRegexp || !preg_match($this->classValidatorRegexp, $className)) {
if (null !== $this->classValidatorRegexp && !preg_match($this->classValidatorRegexp, $className)) {
return false;
}

Expand Down
Expand Up @@ -59,6 +59,10 @@ public function process(ContainerBuilder $container)
$validatorBuilder = $container->getDefinition($this->validatorBuilderService);
foreach ($container->findTaggedServiceIds($this->tag) as $id => $tags) {
$regexp = $this->getRegexp(array_merge($globalNamespaces, $servicesToNamespaces[$id] ?? []));
if (null === $regexp) {
$container->removeDefinition($id);
continue;
}

$container->getDefinition($id)->setArgument('$classValidatorRegexp', $regexp);
$validatorBuilder->addMethodCall('addLoader', [new Reference($id)]);
Expand Down
Expand Up @@ -81,6 +81,6 @@ public function testDoNotMapAllClassesWhenConfigIsEmpty()

(new AddAutoMappingConfigurationPass())->process($container);

$this->assertNull($container->getDefinition('loader')->getArgument('$classValidatorRegexp'));
$this->assertFalse($container->hasDefinition('loader'));
}
}

0 comments on commit ee4b99f

Please sign in to comment.