Choice.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 static $errorNames = [
  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. public $choices;
  30. public $callback;
  31. public $multiple = false;
  32. public $strict = true;
  33. public $min;
  34. public $max;
  35. public $message = 'The value you selected is not a valid choice.';
  36. public $multipleMessage = 'One or more of the given values is invalid.';
  37. public $minMessage = 'You must select at least {{ limit }} choice.|You must select at least {{ limit }} choices.';
  38. public $maxMessage = 'You must select at most {{ limit }} choice.|You must select at most {{ limit }} choices.';
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function getDefaultOption()
  43. {
  44. return 'choices';
  45. }
  46. public function __construct(
  47. $choices = null,
  48. $callback = null,
  49. bool $multiple = null,
  50. bool $strict = null,
  51. int $min = null,
  52. int $max = null,
  53. string $message = null,
  54. string $multipleMessage = null,
  55. string $minMessage = null,
  56. string $maxMessage = null,
  57. $groups = null,
  58. $payload = null,
  59. array $options = []
  60. ) {
  61. if (\is_array($choices) && \is_string(key($choices))) {
  62. $options = array_merge($choices, $options);
  63. } elseif (null !== $choices) {
  64. $options['value'] = $choices;
  65. }
  66. parent::__construct($options, $groups, $payload);
  67. $this->callback = $callback ?? $this->callback;
  68. $this->multiple = $multiple ?? $this->multiple;
  69. $this->strict = $strict ?? $this->strict;
  70. $this->min = $min ?? $this->min;
  71. $this->max = $max ?? $this->max;
  72. $this->message = $message ?? $this->message;
  73. $this->multipleMessage = $multipleMessage ?? $this->multipleMessage;
  74. $this->minMessage = $minMessage ?? $this->minMessage;
  75. $this->maxMessage = $maxMessage ?? $this->maxMessage;
  76. }
  77. }