Skip to content

Commit

Permalink
Added a validator initializer for the canonical fields.
Browse files Browse the repository at this point in the history
These fields need to be updated before validating their uniqueness.
  • Loading branch information
stof committed Jul 8, 2012
1 parent 7c46cd9 commit 4878c6e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Resources/config/validator.xml
Expand Up @@ -10,6 +10,11 @@
</parameters>

<services>
<service id="fos_user.validator.initializer" class="FOS\UserBundle\Validator\Initializer" public="false">
<tag name="validator.initializer" />
<argument type="service" id="fos_user.user_manager" />
</service>

<!-- Unique Validator Service -->
<service id="fos_user.validator.unique" class="%fos_user.validator.unique.class%">
<argument type="service" id="fos_user.user_manager" />
Expand Down
38 changes: 38 additions & 0 deletions Validator/Initializer.php
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\UserBundle\Validator;

use FOS\UserBundle\Model\UserInterface;
use FOS\UserBundle\Model\UserManagerInterface;
use Symfony\Component\Validator\ObjectInitializerInterface;

/**
* Automatically updates the canonical fields before validation.
*
* @author Christophe Coevoet <stof@notk.org>
*/
class Initializer implements ObjectInitializerInterface
{
private $userManager;

public function __construct(UserManagerInterface $userManager)
{
$this->userManager = $userManager;
}

public function initialize($object)
{
if ($object instanceof UserInterface) {
$this->userManager->updateCanonicalFields($object);
}
}
}

0 comments on commit 4878c6e

Please sign in to comment.