Get entity manager in custom validator
<p>I would like to know if it is possible to get the entity manager to reach repository in a custom validator.</p>

<p>I tried to declare the validator as a service and give the "@doctrine.orm.default_entity_manager" argument.</p>

<p>But of course it does not work because, obviously I dont call the service when I add my validator as an annotation in the entity.</p>

<p>Is there a way to reach the repository in my custom validator that I called via an annotation in the entity to validate?</p>

<pre><code>use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator

class UniqueKeyValidator extends ConstraintValidator {
  public function validate($value, Constraint $constraint) {
    $entity = $this-&gt;context-&gt;getRoot()-&gt;getData();
    ... calling entity repository .....
  }
}
</code></pre>

<p>My service :</p>

<pre><code>app.validator.unique:
    class: MyBundle\Validator\Constraints\UniqueKeyValidator
    arguments: ["@doctrine.orm.default_entity_manager"]
    tags:
        - { name: validator.constraint_validator }
</code></pre>