entityManager = $entityManager; $this->enforceUniqueIpn = $enforceUniqueIpn; } public function validate($value, Constraint $constraint) { if (null === $value || '' === $value) { return; } $repository = $this->entityManager->getRepository(Part::class); $existingPart = $repository->findOneBy(['ipn' => $value]); if ($existingPart) { if ($this->enforceUniqueIpn) { $this->context->buildViolation($constraint->message) ->setParameter('{{ value }}', $value) ->addViolation(); } } } }