StringValidator.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 int|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 int 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 int 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 ? Yii::$app->charset : 'UTF-8';
  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. ValidationAsset::register($view);
  142. $options = $this->getClientOptions($model, $attribute);
  143. return 'yii.validation.string(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');';
  144. }
  145. /**
  146. * {@inheritdoc}
  147. */
  148. public function getClientOptions($model, $attribute)
  149. {
  150. $label = $model->getAttributeLabel($attribute);
  151. $options = [
  152. 'message' => $this->formatMessage($this->message, [
  153. 'attribute' => $label,
  154. ]),
  155. ];
  156. if ($this->min !== null) {
  157. $options['min'] = $this->min;
  158. $options['tooShort'] = $this->formatMessage($this->tooShort, [
  159. 'attribute' => $label,
  160. 'min' => $this->min,
  161. ]);
  162. }
  163. if ($this->max !== null) {
  164. $options['max'] = $this->max;
  165. $options['tooLong'] = $this->formatMessage($this->tooLong, [
  166. 'attribute' => $label,
  167. 'max' => $this->max,
  168. ]);
  169. }
  170. if ($this->length !== null) {
  171. $options['is'] = $this->length;
  172. $options['notEqual'] = $this->formatMessage($this->notEqual, [
  173. 'attribute' => $label,
  174. 'length' => $this->length,
  175. ]);
  176. }
  177. if ($this->skipOnEmpty) {
  178. $options['skipOnEmpty'] = 1;
  179. }
  180. return $options;
  181. }
  182. }