vendor/symfony/validator/Constraints/Choice.php line 23

  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\Component\Validator\Constraints;
  11. use Symfony\Component\Validator\Constraint;
  12. /**
  13.  * @Annotation
  14.  * @Target({"PROPERTY", "METHOD", "ANNOTATION"})
  15.  *
  16.  * @author Bernhard Schussek <bschussek@gmail.com>
  17.  */
  18. #[\Attribute(\Attribute::TARGET_PROPERTY \Attribute::TARGET_METHOD \Attribute::IS_REPEATABLE)]
  19. class Choice extends Constraint
  20. {
  21.     public const NO_SUCH_CHOICE_ERROR '8e179f1b-97aa-4560-a02f-2a8b42e49df7';
  22.     public const TOO_FEW_ERROR '11edd7eb-5872-4b6e-9f12-89923999fd0e';
  23.     public const TOO_MANY_ERROR '9bd98e49-211c-433f-8630-fd1c2d0f08c3';
  24.     protected const ERROR_NAMES = [
  25.         self::NO_SUCH_CHOICE_ERROR => 'NO_SUCH_CHOICE_ERROR',
  26.         self::TOO_FEW_ERROR => 'TOO_FEW_ERROR',
  27.         self::TOO_MANY_ERROR => 'TOO_MANY_ERROR',
  28.     ];
  29.     /**
  30.      * @deprecated since Symfony 6.1, use const ERROR_NAMES instead
  31.      */
  32.     protected static $errorNames self::ERROR_NAMES;
  33.     public $choices;
  34.     public $callback;
  35.     public $multiple false;
  36.     public $strict true;
  37.     public $min;
  38.     public $max;
  39.     public $message 'The value you selected is not a valid choice.';
  40.     public $multipleMessage 'One or more of the given values is invalid.';
  41.     public $minMessage 'You must select at least {{ limit }} choice.|You must select at least {{ limit }} choices.';
  42.     public $maxMessage 'You must select at most {{ limit }} choice.|You must select at most {{ limit }} choices.';
  43.     public bool $match true;
  44.     public function getDefaultOption(): ?string
  45.     {
  46.         return 'choices';
  47.     }
  48.     public function __construct(
  49.         string|array $options = [],
  50.         array $choices null,
  51.         callable|string $callback null,
  52.         bool $multiple null,
  53.         bool $strict null,
  54.         int $min null,
  55.         int $max null,
  56.         string $message null,
  57.         string $multipleMessage null,
  58.         string $minMessage null,
  59.         string $maxMessage null,
  60.         array $groups null,
  61.         mixed $payload null,
  62.         bool $match null,
  63.     ) {
  64.         if (\is_array($options) && $options && array_is_list($options)) {
  65.             $choices ??= $options;
  66.             $options = [];
  67.         }
  68.         if (null !== $choices) {
  69.             $options['value'] = $choices;
  70.         }
  71.         parent::__construct($options$groups$payload);
  72.         $this->callback $callback ?? $this->callback;
  73.         $this->multiple $multiple ?? $this->multiple;
  74.         $this->strict $strict ?? $this->strict;
  75.         $this->min $min ?? $this->min;
  76.         $this->max $max ?? $this->max;
  77.         $this->message $message ?? $this->message;
  78.         $this->multipleMessage $multipleMessage ?? $this->multipleMessage;
  79.         $this->minMessage $minMessage ?? $this->minMessage;
  80.         $this->maxMessage $maxMessage ?? $this->maxMessage;
  81.         $this->match $match ?? $this->match;
  82.     }
  83. }