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

  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.      * @param array|string $fields the combination of fields that must contain unique values or a set of options
  41.      */
  42.     public function __construct(
  43.         $fields,
  44.         string $message null,
  45.         string $service null,
  46.         string $em null,
  47.         string $entityClass null,
  48.         string $repositoryMethod null,
  49.         string $errorPath null,
  50.         bool $ignoreNull null,
  51.         array $groups null,
  52.         $payload null,
  53.         array $options = []
  54.     ) {
  55.         if (\is_array($fields) && \is_string(key($fields))) {
  56.             $options array_merge($fields$options);
  57.         } elseif (null !== $fields) {
  58.             $options['fields'] = $fields;
  59.         }
  60.         parent::__construct($options$groups$payload);
  61.         $this->message $message ?? $this->message;
  62.         $this->service $service ?? $this->service;
  63.         $this->em $em ?? $this->em;
  64.         $this->entityClass $entityClass ?? $this->entityClass;
  65.         $this->repositoryMethod $repositoryMethod ?? $this->repositoryMethod;
  66.         $this->errorPath $errorPath ?? $this->errorPath;
  67.         $this->ignoreNull $ignoreNull ?? $this->ignoreNull;
  68.     }
  69.     public function getRequiredOptions(): array
  70.     {
  71.         return ['fields'];
  72.     }
  73.     /**
  74.      * The validator must be defined as a service with this name.
  75.      */
  76.     public function validatedBy(): string
  77.     {
  78.         return $this->service;
  79.     }
  80.     public function getTargets(): string|array
  81.     {
  82.         return self::CLASS_CONSTRAINT;
  83.     }
  84.     public function getDefaultOption(): ?string
  85.     {
  86.         return 'fields';
  87.     }
  88. }