Skip to content

Commit 644a645

Browse files
committed
Split validation specific to the storage
1 parent 7dbd45b commit 644a645

File tree

6 files changed

+83
-14
lines changed

6 files changed

+83
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Sonata Project package.
7+
*
8+
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Sonata\UserBundle\DependencyInjection\Compiler;
15+
16+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17+
use Symfony\Component\DependencyInjection\ContainerBuilder;
18+
19+
/**
20+
* @internal
21+
*/
22+
final class ValidationCompilerPass implements CompilerPassInterface
23+
{
24+
public function process(ContainerBuilder $container): void
25+
{
26+
if (!$container->hasParameter('sonata.user.manager_type')
27+
|| !$container->hasDefinition('validator.builder')) {
28+
return;
29+
}
30+
31+
$managerType = $container->getParameter('sonata.user.manager_type');
32+
\assert(\is_string($managerType));
33+
34+
$container->getDefinition('validator.builder')->addMethodCall('addXmlMapping', [
35+
__DIR__.'/../../Resources/config/storage-validation/'.$managerType.'.xml',
36+
]);
37+
}
38+
}

src/DependencyInjection/SonataUserExtension.php

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public function load(array $configs, ContainerBuilder $container): void
5353
}
5454

5555
$loader->load(sprintf('%s.php', $config['manager_type']));
56+
$container->setParameter('sonata.user.manager_type', $config['manager_type']);
5657

5758
$loader->load('twig.php');
5859
$loader->load('commands.php');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
3+
<class name="Sonata\UserBundle\Model\User">
4+
<constraint name="Doctrine\Bundle\MongoDBBundle\Validator\Constraints\Unique">
5+
<option name="fields">email</option>
6+
<option name="errorPath">emailCanonical</option>
7+
<option name="groups">
8+
<value>Registration</value>
9+
<value>Profile</value>
10+
</option>
11+
</constraint>
12+
<constraint name="Doctrine\Bundle\MongoDBBundle\Validator\Constraints\Unique">
13+
<option name="fields">username</option>
14+
<option name="errorPath">usernameCanonical</option>
15+
<option name="groups">
16+
<value>Registration</value>
17+
<value>Profile</value>
18+
</option>
19+
</constraint>
20+
</class>
21+
</constraint-mapping>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
3+
<class name="Sonata\UserBundle\Model\User">
4+
<constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
5+
<option name="fields">emailCanonical</option>
6+
<option name="errorPath">email</option>
7+
<option name="groups">
8+
<value>Registration</value>
9+
<value>Profile</value>
10+
</option>
11+
</constraint>
12+
<constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
13+
<option name="fields">usernameCanonical</option>
14+
<option name="errorPath">username</option>
15+
<option name="groups">
16+
<value>Registration</value>
17+
<value>Profile</value>
18+
</option>
19+
</constraint>
20+
</class>
21+
</constraint-mapping>

src/Resources/config/validation.xml

-14
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
33
<class name="Sonata\UserBundle\Model\User">
4-
<constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
5-
<option name="fields">email</option>
6-
<option name="groups">
7-
<value>Registration</value>
8-
<value>Profile</value>
9-
</option>
10-
</constraint>
11-
<constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
12-
<option name="fields">username</option>
13-
<option name="groups">
14-
<value>Registration</value>
15-
<value>Profile</value>
16-
</option>
17-
</constraint>
184
<property name="username">
195
<constraint name="NotBlank">
206
<option name="groups">

src/SonataUserBundle.php

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use Sonata\UserBundle\DependencyInjection\Compiler\GlobalVariablesCompilerPass;
1717
use Sonata\UserBundle\DependencyInjection\Compiler\RolesMatrixCompilerPass;
18+
use Sonata\UserBundle\DependencyInjection\Compiler\ValidationCompilerPass;
1819
use Symfony\Component\DependencyInjection\ContainerBuilder;
1920
use Symfony\Component\HttpKernel\Bundle\Bundle;
2021

@@ -24,5 +25,6 @@ public function build(ContainerBuilder $container): void
2425
{
2526
$container->addCompilerPass(new GlobalVariablesCompilerPass());
2627
$container->addCompilerPass(new RolesMatrixCompilerPass());
28+
$container->addCompilerPass(new ValidationCompilerPass());
2729
}
2830
}

0 commit comments

Comments
 (0)