Skip to content

Commit

Permalink
Split validation specific to the storage
Browse files Browse the repository at this point in the history
  • Loading branch information
jordisala1991 committed Apr 9, 2023
1 parent 7dbd45b commit dafad30
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 14 deletions.
42 changes: 42 additions & 0 deletions src/DependencyInjection/Compiler/ValidationCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\UserBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* @internal
*/
final class ValidationCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if (!$container->hasParameter('sonata.user.manager_type')
|| !$container->hasDefinition('validator.builder')) {
return;
}

$managerType = $container->getParameter('sonata.user.manager_type');
\assert(\is_string($managerType));

$container->getDefinition('validator.builder')->addMethodCall('addXmlMapping', [
sprintf(
'%s/../../Resources/config/storage-validation/%s.xml',
__DIR__,
$managerType
),
]);
}
}
1 change: 1 addition & 0 deletions src/DependencyInjection/SonataUserExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function load(array $configs, ContainerBuilder $container): void
}

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

$loader->load('twig.php');
$loader->load('commands.php');
Expand Down
21 changes: 21 additions & 0 deletions src/Resources/config/storage-validation/mongodb.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<class name="Sonata\UserBundle\Model\User">
<constraint name="Doctrine\Bundle\MongoDBBundle\Validator\Constraints\Unique">
<option name="fields">email</option>
<option name="errorPath">emailCanonical</option>
<option name="groups">
<value>Registration</value>
<value>Profile</value>
</option>
</constraint>
<constraint name="Doctrine\Bundle\MongoDBBundle\Validator\Constraints\Unique">
<option name="fields">username</option>
<option name="errorPath">usernameCanonical</option>
<option name="groups">
<value>Registration</value>
<value>Profile</value>
</option>
</constraint>
</class>
</constraint-mapping>
21 changes: 21 additions & 0 deletions src/Resources/config/storage-validation/orm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<class name="Sonata\UserBundle\Model\User">
<constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
<option name="fields">emailCanonical</option>
<option name="errorPath">email</option>
<option name="groups">
<value>Registration</value>
<value>Profile</value>
</option>
</constraint>
<constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
<option name="fields">usernameCanonical</option>
<option name="errorPath">username</option>
<option name="groups">
<value>Registration</value>
<value>Profile</value>
</option>
</constraint>
</class>
</constraint-mapping>
14 changes: 0 additions & 14 deletions src/Resources/config/validation.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<class name="Sonata\UserBundle\Model\User">
<constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
<option name="fields">email</option>
<option name="groups">
<value>Registration</value>
<value>Profile</value>
</option>
</constraint>
<constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
<option name="fields">username</option>
<option name="groups">
<value>Registration</value>
<value>Profile</value>
</option>
</constraint>
<property name="username">
<constraint name="NotBlank">
<option name="groups">
Expand Down
2 changes: 2 additions & 0 deletions src/SonataUserBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Sonata\UserBundle\DependencyInjection\Compiler\GlobalVariablesCompilerPass;
use Sonata\UserBundle\DependencyInjection\Compiler\RolesMatrixCompilerPass;
use Sonata\UserBundle\DependencyInjection\Compiler\ValidationCompilerPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

Expand All @@ -24,5 +25,6 @@ public function build(ContainerBuilder $container): void
{
$container->addCompilerPass(new GlobalVariablesCompilerPass());
$container->addCompilerPass(new RolesMatrixCompilerPass());
$container->addCompilerPass(new ValidationCompilerPass());
}
}

0 comments on commit dafad30

Please sign in to comment.