EDIT: SOLVED
I am trying to create a special validator that works as a service (mainly to get entity manager).
I have been following the document and some blog posts, but cannot make it work. I am not getting an error, but the isValid method from my restriction is never called.
The My Entity class (which should be verified using "UniqueLem" (note that the Doctrine validator works):
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;
class Project{...
My class of restrictions:
namespace Elyotech \ AdminBundle \ Validator;
use Symfony \ Component \ Validator \ Constraint;
class UniqueLem extends Constraint
{
public $message = 'This value is already used';
public $entity;
public $property;
public function validatedBy()
{
die;
return 'validator.unique';
}
public function getTargets()
{
return self::CLASS_CONSTRAINT;
}
validator
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->entityManager = $entityManager;
}
public function isValid($value, Constraint $constraint) {
$this->setMessage($constraint->message);
return false;
}
Service Announcement:
my.validator.unique:
class: %my.validator.unique.class%
arguments: [@doctrine.orm.entity_manager]
tags:
- { name: validator.constraint_validator, alias: validator.unique }
Thus, the problem does not arise from the service, since I never reach "die" in the UniqueLem / isValid () function.
Thank!