vendor/symfony/doctrine-bridge/Validator/Constraints/UniqueEntity.php line 52

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bridge\Doctrine\Validator\Constraints;
  11. use Symfony\Component\Validator\Constraint;
  12. /**
  13.  * Constraint for the Unique Entity validator.
  14.  *
  15.  * @Annotation
  16.  * @Target({"CLASS", "ANNOTATION"})
  17.  *
  18.  * @author Benjamin Eberlei <kontakt@beberlei.de>
  19.  */
  20. #[\Attribute(\Attribute::TARGET_CLASS \Attribute::IS_REPEATABLE)]
  21. class UniqueEntity extends Constraint
  22. {
  23.     public const NOT_UNIQUE_ERROR '23bd9dbf-6b9b-41cd-a99e-4844bcf3077f';
  24.     protected const ERROR_NAMES = [
  25.         self::NOT_UNIQUE_ERROR => 'NOT_UNIQUE_ERROR',
  26.     ];
  27.     public $message 'This value is already used.';
  28.     public $service 'doctrine.orm.validator.unique';
  29.     public $em null;
  30.     public $entityClass null;
  31.     public $repositoryMethod 'findBy';
  32.     public $fields = [];
  33.     public $errorPath null;
  34.     public $ignoreNull true;
  35.     /**
  36.      * @deprecated since Symfony 6.1, use const ERROR_NAMES instead
  37.      */
  38.     protected static $errorNames self::ERROR_NAMES;
  39.     /**
  40.      * {@inheritdoc}
  41.      *
  42.      * @param array|string $fields the combination of fields that must contain unique values or a set of options
  43.      */
  44.     public function __construct(
  45.         $fields,
  46.         string $message null,
  47.         string $service null,
  48.         string $em null,
  49.         string $entityClass null,
  50.         string $repositoryMethod null,
  51.         string $errorPath null,
  52.         bool $ignoreNull null,
  53.         array $groups null,
  54.         $payload null,
  55.         array $options = []
  56.     ) {
  57.         if (\is_array($fields) && \is_string(key($fields))) {
  58.             $options array_merge($fields$options);
  59.         } elseif (null !== $fields) {
  60.             $options['fields'] = $fields;
  61.         }
  62.         parent::__construct($options$groups$payload);
  63.         $this->message $message ?? $this->message;
  64.         $this->service $service ?? $this->service;
  65.         $this->em $em ?? $this->em;
  66.         $this->entityClass $entityClass ?? $this->entityClass;
  67.         $this->repositoryMethod $repositoryMethod ?? $this->repositoryMethod;
  68.         $this->errorPath $errorPath ?? $this->errorPath;
  69.         $this->ignoreNull $ignoreNull ?? $this->ignoreNull;
  70.     }
  71.     public function getRequiredOptions(): array
  72.     {
  73.         return ['fields'];
  74.     }
  75.     /**
  76.      * The validator must be defined as a service with this name.
  77.      */
  78.     public function validatedBy(): string
  79.     {
  80.         return $this->service;
  81.     }
  82.     /**
  83.      * {@inheritdoc}
  84.      */
  85.     public function getTargets(): string|array
  86.     {
  87.         return self::CLASS_CONSTRAINT;
  88.     }
  89.     public function getDefaultOption(): ?string
  90.     {
  91.         return 'fields';
  92.     }
  93. }