Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony2 custom validator as service not loaded
    primarykey
    data
    text
    <p><strong>EDIT : SOLVED</strong></p> <p>I'm trying to build a custom validator running as a service (mainly for getting the entity manager).</p> <p>I followed the doc and some blog posts but can't make it working. I don't get any error message but the method isValid from my constraint is never called.</p> <p><strong>My Entity class (that has to be validated by "UniqueLem" (note that the Doctrine validator works):</strong></p> <pre><code>namespace Elyotech\VersionManagerBundle\Entity; use Gedmo\Mapping\Annotation as Gedmo; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; use Me\AdminBundle\Validator\UniqueLem; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; /** * @ORM\Entity(repositoryClass="Me\VersionManagerBundle\Repository\ProjectRepository") * @ORM\Table(name="project") * @UniqueLem * @UniqueEntity("name") */ class Project{... </code></pre> <p><strong>My Constraint class:</strong> namespace Elyotech\AdminBundle\Validator;</p> <p>use Symfony\Component\Validator\Constraint;</p> <pre><code>/* * @Annotation * */ class UniqueLem extends Constraint { public $message = 'This value is already used'; public $entity; public $property; public function validatedBy() { die; // Test to check if function is called return 'validator.unique'; } public function getTargets() { return self::CLASS_CONSTRAINT; } </code></pre> <p><strong>Validator</strong></p> <pre><code> namespace Elyotech\AdminBundle\Validator; use Doctrine\ORM\EntityManager; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; class UniqueValidator extends ConstraintValidator { private $entityManager; public function __construct( $entityManager) { $this-&gt;entityManager = $entityManager; } public function isValid($value, Constraint $constraint) { $this-&gt;setMessage($constraint-&gt;message); return false; } </code></pre> <p><strong>Service declaration:</strong></p> <pre><code> my.validator.unique: class: %my.validator.unique.class% arguments: [@doctrine.orm.entity_manager] tags: - { name: validator.constraint_validator, alias: validator.unique } </code></pre> <p>So the issue doesn't seem to be coming from the service as I never reach the "die" in the UniqueLem/isValid() function.</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload