選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

StringValidator.php 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. /**
  10. * StringValidator validates that the attribute value is of certain length.
  11. *
  12. * Note, this validator should only be used with string-typed attributes.
  13. *
  14. * @author Qiang Xue <qiang.xue@gmail.com>
  15. * @since 2.0
  16. */
  17. class StringValidator extends Validator
  18. {
  19. /**
  20. * @var integer|array specifies the length limit of the value to be validated.
  21. * This can be specified in one of the following forms:
  22. *
  23. * - an integer: the exact length that the value should be of;
  24. * - an array of one element: the minimum length that the value should be of. For example, `[8]`.
  25. * This will overwrite [[min]].
  26. * - an array of two elements: the minimum and maximum lengths that the value should be of.
  27. * For example, `[8, 128]`. This will overwrite both [[min]] and [[max]].
  28. * @see tooShort for the customized message for a too short string.
  29. * @see tooLong for the customized message for a too long string.
  30. * @see notEqual for the customized message for a string that does not match desired length.
  31. */
  32. public $length;
  33. /**
  34. * @var integer maximum length. If not set, it means no maximum length limit.
  35. * @see tooLong for the customized message for a too long string.
  36. */
  37. public $max;
  38. /**
  39. * @var integer minimum length. If not set, it means no minimum length limit.
  40. * @see tooShort for the customized message for a too short string.
  41. */
  42. public $min;
  43. /**
  44. * @var string user-defined error message used when the value is not a string.
  45. */
  46. public $message;
  47. /**
  48. * @var string user-defined error message used when the length of the value is smaller than [[min]].
  49. */
  50. public $tooShort;
  51. /**
  52. * @var string user-defined error message used when the length of the value is greater than [[max]].
  53. */
  54. public $tooLong;
  55. /**
  56. * @var string user-defined error message used when the length of the value is not equal to [[length]].
  57. */
  58. public $notEqual;
  59. /**
  60. * @var string the encoding of the string value to be validated (e.g. 'UTF-8').
  61. * If this property is not set, [[\yii\base\Application::charset]] will be used.
  62. */
  63. public $encoding;
  64. /**
  65. * @inheritdoc
  66. */
  67. public function init()
  68. {
  69. parent::init();
  70. if (is_array($this->length)) {
  71. if (isset($this->length[0])) {
  72. $this->min = $this->length[0];
  73. }
  74. if (isset($this->length[1])) {
  75. $this->max = $this->length[1];
  76. }
  77. $this->length = null;
  78. }
  79. if ($this->encoding === null) {
  80. $this->encoding = Yii::$app->charset;
  81. }
  82. if ($this->message === null) {
  83. $this->message = Yii::t('yii', '{attribute} must be a string.');
  84. }
  85. if ($this->min !== null && $this->tooShort === null) {
  86. $this->tooShort = Yii::t('yii', '{attribute} should contain at least {min, number} {min, plural, one{character} other{characters}}.');
  87. }
  88. if ($this->max !== null && $this->tooLong === null) {
  89. $this->tooLong = Yii::t('yii', '{attribute} should contain at most {max, number} {max, plural, one{character} other{characters}}.');
  90. }
  91. if ($this->length !== null && $this->notEqual === null) {
  92. $this->notEqual = Yii::t('yii', '{attribute} should contain {length, number} {length, plural, one{character} other{characters}}.');
  93. }
  94. }
  95. /**
  96. * @inheritdoc
  97. */
  98. public function validateAttribute($model, $attribute)
  99. {
  100. $value = $model->$attribute;
  101. if (!is_string($value)) {
  102. $this->addError($model, $attribute, $this->message);
  103. return;
  104. }
  105. $length = mb_strlen($value, $this->encoding);
  106. if ($this->min !== null && $length < $this->min) {
  107. $this->addError($model, $attribute, $this->tooShort, ['min' => $this->min]);
  108. }
  109. if ($this->max !== null && $length > $this->max) {
  110. $this->addError($model, $attribute, $this->tooLong, ['max' => $this->max]);
  111. }
  112. if ($this->length !== null && $length !== $this->length) {
  113. $this->addError($model, $attribute, $this->notEqual, ['length' => $this->length]);
  114. }
  115. }
  116. /**
  117. * @inheritdoc
  118. */
  119. protected function validateValue($value)
  120. {
  121. if (!is_string($value)) {
  122. return [$this->message, []];
  123. }
  124. $length = mb_strlen($value, $this->encoding);
  125. if ($this->min !== null && $length < $this->min) {
  126. return [$this->tooShort, ['min' => $this->min]];
  127. }
  128. if ($this->max !== null && $length > $this->max) {
  129. return [$this->tooLong, ['max' => $this->max]];
  130. }
  131. if ($this->length !== null && $length !== $this->length) {
  132. return [$this->notEqual, ['length' => $this->length]];
  133. }
  134. return null;
  135. }
  136. /**
  137. * @inheritdoc
  138. */
  139. public function clientValidateAttribute($model, $attribute, $view)
  140. {
  141. $label = $model->getAttributeLabel($attribute);
  142. $options = [
  143. 'message' => Yii::$app->getI18n()->format($this->message, [
  144. 'attribute' => $label,
  145. ], Yii::$app->language),
  146. ];
  147. if ($this->min !== null) {
  148. $options['min'] = $this->min;
  149. $options['tooShort'] = Yii::$app->getI18n()->format($this->tooShort, [
  150. 'attribute' => $label,
  151. 'min' => $this->min,
  152. ], Yii::$app->language);
  153. }
  154. if ($this->max !== null) {
  155. $options['max'] = $this->max;
  156. $options['tooLong'] = Yii::$app->getI18n()->format($this->tooLong, [
  157. 'attribute' => $label,
  158. 'max' => $this->max,
  159. ], Yii::$app->language);
  160. }
  161. if ($this->length !== null) {
  162. $options['is'] = $this->length;
  163. $options['notEqual'] = Yii::$app->getI18n()->format($this->notEqual, [
  164. 'attribute' => $label,
  165. 'length' => $this->length,
  166. ], Yii::$app->language);
  167. }
  168. if ($this->skipOnEmpty) {
  169. $options['skipOnEmpty'] = 1;
  170. }
  171. ValidationAsset::register($view);
  172. return 'yii.validation.string(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');';
  173. }
  174. }