UniqueEntity.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. public $message = 'This value is already used.';
  25. public $service = 'doctrine.orm.validator.unique';
  26. public $em = null;
  27. public $entityClass = null;
  28. public $repositoryMethod = 'findBy';
  29. public $fields = [];
  30. public $errorPath = null;
  31. public $ignoreNull = true;
  32. protected static $errorNames = [
  33. self::NOT_UNIQUE_ERROR => 'NOT_UNIQUE_ERROR',
  34. ];
  35. /**
  36. * {@inheritdoc}
  37. *
  38. * @param array|string $fields the combination of fields that must contain unique values or a set of options
  39. */
  40. public function __construct(
  41. $fields,
  42. string $message = null,
  43. string $service = null,
  44. string $em = null,
  45. string $entityClass = null,
  46. string $repositoryMethod = null,
  47. string $errorPath = null,
  48. bool $ignoreNull = null,
  49. array $groups = null,
  50. $payload = null,
  51. array $options = []
  52. ) {
  53. if (\is_array($fields) && \is_string(key($fields))) {
  54. $options = array_merge($fields, $options);
  55. } elseif (null !== $fields) {
  56. $options['fields'] = $fields;
  57. }
  58. parent::__construct($options, $groups, $payload);
  59. $this->message = $message ?? $this->message;
  60. $this->service = $service ?? $this->service;
  61. $this->em = $em ?? $this->em;
  62. $this->entityClass = $entityClass ?? $this->entityClass;
  63. $this->repositoryMethod = $repositoryMethod ?? $this->repositoryMethod;
  64. $this->errorPath = $errorPath ?? $this->errorPath;
  65. $this->ignoreNull = $ignoreNull ?? $this->ignoreNull;
  66. }
  67. public function getRequiredOptions()
  68. {
  69. return ['fields'];
  70. }
  71. /**
  72. * The validator must be defined as a service with this name.
  73. *
  74. * @return string
  75. */
  76. public function validatedBy()
  77. {
  78. return $this->service;
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function getTargets()
  84. {
  85. return self::CLASS_CONSTRAINT;
  86. }
  87. public function getDefaultOption()
  88. {
  89. return 'fields';
  90. }
  91. }