RangeValidator.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * @link https://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license https://www.yiiframework.com/license/
  6. */
  7. namespace yii\validators;
  8. use Yii;
  9. use yii\base\InvalidConfigException;
  10. use yii\helpers\ArrayHelper;
  11. use yii\helpers\Json;
  12. /**
  13. * RangeValidator validates that the attribute value is among a list of values.
  14. *
  15. * The range can be specified via the [[range]] property.
  16. * If the [[not]] property is set true, the validator will ensure the attribute value
  17. * is NOT among the specified range.
  18. *
  19. * @author Qiang Xue <qiang.xue@gmail.com>
  20. * @since 2.0
  21. */
  22. class RangeValidator extends Validator
  23. {
  24. /**
  25. * @var array|\Traversable|\Closure a list of valid values that the attribute value should be among or an anonymous function that returns
  26. * such a list. The signature of the anonymous function should be as follows,
  27. *
  28. * ```php
  29. * function($model, $attribute) {
  30. * // compute range
  31. * return $range;
  32. * }
  33. * ```
  34. */
  35. public $range;
  36. /**
  37. * @var bool whether the comparison is strict (both type and value must be the same)
  38. */
  39. public $strict = false;
  40. /**
  41. * @var bool whether to invert the validation logic. Defaults to false. If set to true,
  42. * the attribute value should NOT be among the list of values defined via [[range]].
  43. */
  44. public $not = false;
  45. /**
  46. * @var bool whether to allow array type attribute.
  47. */
  48. public $allowArray = false;
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function init()
  53. {
  54. parent::init();
  55. if (
  56. !is_array($this->range)
  57. && !($this->range instanceof \Closure)
  58. && !($this->range instanceof \Traversable)
  59. ) {
  60. throw new InvalidConfigException('The "range" property must be set.');
  61. }
  62. if ($this->message === null) {
  63. $this->message = Yii::t('yii', '{attribute} is invalid.');
  64. }
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. protected function validateValue($value)
  70. {
  71. $in = false;
  72. if (
  73. $this->allowArray
  74. && ($value instanceof \Traversable || is_array($value))
  75. && ArrayHelper::isSubset($value, $this->range, $this->strict)
  76. ) {
  77. $in = true;
  78. }
  79. if (!$in && ArrayHelper::isIn($value, $this->range, $this->strict)) {
  80. $in = true;
  81. }
  82. return $this->not !== $in ? null : [$this->message, []];
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function validateAttribute($model, $attribute)
  88. {
  89. if ($this->range instanceof \Closure) {
  90. $this->range = call_user_func($this->range, $model, $attribute);
  91. }
  92. parent::validateAttribute($model, $attribute);
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function clientValidateAttribute($model, $attribute, $view)
  98. {
  99. if ($this->range instanceof \Closure) {
  100. $this->range = call_user_func($this->range, $model, $attribute);
  101. }
  102. ValidationAsset::register($view);
  103. $options = $this->getClientOptions($model, $attribute);
  104. return 'yii.validation.range(value, messages, ' . Json::htmlEncode($options) . ');';
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function getClientOptions($model, $attribute)
  110. {
  111. $range = [];
  112. foreach ($this->range as $value) {
  113. $range[] = (string) $value;
  114. }
  115. $options = [
  116. 'range' => $range,
  117. 'not' => $this->not,
  118. 'message' => $this->formatMessage($this->message, [
  119. 'attribute' => $model->getAttributeLabel($attribute),
  120. ]),
  121. ];
  122. if ($this->skipOnEmpty) {
  123. $options['skipOnEmpty'] = 1;
  124. }
  125. if ($this->allowArray) {
  126. $options['allowArray'] = 1;
  127. }
  128. return $options;
  129. }
  130. }