You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RangeValidator.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\validators;
  8. use Yii;
  9. use yii\base\InvalidConfigException;
  10. /**
  11. * RangeValidator validates that the attribute value is among a list of values.
  12. *
  13. * The range can be specified via the [[range]] property.
  14. * If the [[not]] property is set true, the validator will ensure the attribute value
  15. * is NOT among the specified range.
  16. *
  17. * @author Qiang Xue <qiang.xue@gmail.com>
  18. * @since 2.0
  19. */
  20. class RangeValidator extends Validator
  21. {
  22. /**
  23. * @var array list of valid values that the attribute value should be among
  24. */
  25. public $range;
  26. /**
  27. * @var boolean whether the comparison is strict (both type and value must be the same)
  28. */
  29. public $strict = false;
  30. /**
  31. * @var boolean whether to invert the validation logic. Defaults to false. If set to true,
  32. * the attribute value should NOT be among the list of values defined via [[range]].
  33. */
  34. public $not = false;
  35. /**
  36. * @var boolean whether to allow array type attribute.
  37. */
  38. public $allowArray = false;
  39. /**
  40. * @inheritdoc
  41. */
  42. public function init()
  43. {
  44. parent::init();
  45. if (!is_array($this->range)) {
  46. throw new InvalidConfigException('The "range" property must be set.');
  47. }
  48. if ($this->message === null) {
  49. $this->message = Yii::t('yii', '{attribute} is invalid.');
  50. }
  51. }
  52. /**
  53. * @inheritdoc
  54. */
  55. protected function validateValue($value)
  56. {
  57. if (!$this->allowArray && is_array($value)) {
  58. return [$this->message, []];
  59. }
  60. $in = true;
  61. foreach ((is_array($value) ? $value : [$value]) as $v) {
  62. if (!in_array($v, $this->range, $this->strict)) {
  63. $in = false;
  64. break;
  65. }
  66. }
  67. return $this->not !== $in ? null : [$this->message, []];
  68. }
  69. /**
  70. * @inheritdoc
  71. */
  72. public function clientValidateAttribute($model, $attribute, $view)
  73. {
  74. $range = [];
  75. foreach ($this->range as $value) {
  76. $range[] = (string) $value;
  77. }
  78. $options = [
  79. 'range' => $range,
  80. 'not' => $this->not,
  81. 'message' => Yii::$app->getI18n()->format($this->message, [
  82. 'attribute' => $model->getAttributeLabel($attribute),
  83. ], Yii::$app->language),
  84. ];
  85. if ($this->skipOnEmpty) {
  86. $options['skipOnEmpty'] = 1;
  87. }
  88. if ($this->allowArray) {
  89. $options['allowArray'] = 1;
  90. }
  91. ValidationAsset::register($view);
  92. return 'yii.validation.range(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');';
  93. }
  94. }