StringValidator.php 5.22 KB
Newer Older
w  
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
w  
Qiang Xue committed
5 6 7
 * @license http://www.yiiframework.com/license/
 */

w  
Qiang Xue committed
8 9
namespace yii\validators;

Qiang Xue committed
10 11
use Yii;

w  
Qiang Xue committed
12
/**
w  
Qiang Xue committed
13
 * StringValidator validates that the attribute value is of certain length.
w  
Qiang Xue committed
14 15 16 17
 *
 * Note, this validator should only be used with string-typed attributes.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
18
 * @since 2.0
w  
Qiang Xue committed
19
 */
w  
Qiang Xue committed
20
class StringValidator extends Validator
w  
Qiang Xue committed
21 22
{
	/**
23 24 25 26
	 * @var integer|array specifies the length limit of the value to be validated.
	 * This can be specified in one of the following forms:
	 *
	 * - an integer: the exact length that the value should be of;
Alexander Makarov committed
27
	 * - an array of one element: the minimum length that the value should be of. For example, `[8]`.
28 29
	 *   This will overwrite [[min]].
	 * - an array of two elements: the minimum and maximum lengths that the value should be of.
Alexander Makarov committed
30
	 *   For example, `[8, 128]`. This will overwrite both [[min]] and [[max]].
w  
Qiang Xue committed
31
	 */
32
	public $length;
w  
Qiang Xue committed
33
	/**
34
	 * @var integer maximum length. If not set, it means no maximum length limit.
w  
Qiang Xue committed
35
	 */
36
	public $max;
w  
Qiang Xue committed
37
	/**
38
	 * @var integer minimum length. If not set, it means no minimum length limit.
w  
Qiang Xue committed
39
	 */
40
	public $min;
w  
Qiang Xue committed
41
	/**
w  
Qiang Xue committed
42 43 44 45 46
	 * @var string user-defined error message used when the value is not a string
	 */
	public $message;
	/**
	 * @var string user-defined error message used when the length of the value is smaller than [[min]].
w  
Qiang Xue committed
47 48 49
	 */
	public $tooShort;
	/**
w  
Qiang Xue committed
50
	 * @var string user-defined error message used when the length of the value is greater than [[max]].
w  
Qiang Xue committed
51 52
	 */
	public $tooLong;
w  
Qiang Xue committed
53
	/**
54
	 * @var string user-defined error message used when the length of the value is not equal to [[length]].
w  
Qiang Xue committed
55 56
	 */
	public $notEqual;
w  
Qiang Xue committed
57
	/**
Qiang Xue committed
58 59
	 * @var string the encoding of the string value to be validated (e.g. 'UTF-8').
	 * If this property is not set, [[\yii\base\Application::charset]] will be used.
w  
Qiang Xue committed
60
	 */
Qiang Xue committed
61 62 63
	public $encoding;


w  
Qiang Xue committed
64
	/**
Qiang Xue committed
65
	 * @inheritdoc
w  
Qiang Xue committed
66
	 */
Qiang Xue committed
67 68 69
	public function init()
	{
		parent::init();
70 71 72 73 74 75 76 77 78
		if (is_array($this->length)) {
			if (isset($this->length[0])) {
				$this->min = $this->length[0];
			}
			if (isset($this->length[1])) {
				$this->max = $this->length[1];
			}
			$this->length = null;
		}
Qiang Xue committed
79 80 81
		if ($this->encoding === null) {
			$this->encoding = Yii::$app->charset;
		}
Qiang Xue committed
82
		if ($this->message === null) {
83
			$this->message = Yii::t('yii', '{attribute} must be a string.');
Qiang Xue committed
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}}.');
Qiang Xue committed
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}}.');
Qiang Xue committed
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}}.');
Qiang Xue committed
93
		}
Qiang Xue committed
94
	}
w  
Qiang Xue committed
95 96

	/**
Qiang Xue committed
97
	 * @inheritdoc
w  
Qiang Xue committed
98
	 */
w  
Qiang Xue committed
99
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
100 101
	{
		$value = $object->$attribute;
w  
Qiang Xue committed
102 103

		if (!is_string($value)) {
Qiang Xue committed
104
			$this->addError($object, $attribute, $this->message);
w  
Qiang Xue committed
105
			return;
w  
Qiang Xue committed
106
		}
w  
Qiang Xue committed
107

Qiang Xue committed
108
		$length = mb_strlen($value, $this->encoding);
w  
Qiang Xue committed
109

w  
Qiang Xue committed
110
		if ($this->min !== null && $length < $this->min) {
111
			$this->addError($object, $attribute, $this->tooShort, ['min' => $this->min]);
w  
Qiang Xue committed
112
		}
w  
Qiang Xue committed
113
		if ($this->max !== null && $length > $this->max) {
114
			$this->addError($object, $attribute, $this->tooLong, ['max' => $this->max]);
w  
Qiang Xue committed
115
		}
116
		if ($this->length !== null && $length !== $this->length) {
117
			$this->addError($object, $attribute, $this->notEqual, ['length' => $this->length]);
w  
Qiang Xue committed
118 119 120
		}
	}

Qiang Xue committed
121
	/**
Qiang Xue committed
122
	 * @inheritdoc
Qiang Xue committed
123
	 */
Qiang Xue committed
124
	protected function validateValue($value)
Qiang Xue committed
125 126
	{
		if (!is_string($value)) {
Qiang Xue committed
127
			return [$this->message, []];
Qiang Xue committed
128
		}
Qiang Xue committed
129

Qiang Xue committed
130
		$length = mb_strlen($value, $this->encoding);
Qiang Xue committed
131 132 133 134 135 136 137 138 139 140 141 142

		if ($this->min !== null && $length < $this->min) {
			return [$this->tooShort, ['min' => $this->min]];
		}
		if ($this->max !== null && $length > $this->max) {
			return [$this->tooLong, ['max' => $this->max]];
		}
		if ($this->length !== null && $length !== $this->length) {
			return [$this->notEqual, ['length' => $this->length]];
		}

		return null;
Qiang Xue committed
143 144
	}

w  
Qiang Xue committed
145
	/**
Qiang Xue committed
146
	 * @inheritdoc
w  
Qiang Xue committed
147
	 */
148
	public function clientValidateAttribute($object, $attribute, $view)
w  
Qiang Xue committed
149 150 151
	{
		$label = $object->getAttributeLabel($attribute);

Alexander Makarov committed
152
		$options = [
153
			'message' => Yii::$app->getI18n()->format($this->message, [
154
				'attribute' => $label,
155
			], Yii::$app->language),
Alexander Makarov committed
156
		];
157

w  
Qiang Xue committed
158
		if ($this->min !== null) {
159
			$options['min'] = $this->min;
160
			$options['tooShort'] = Yii::$app->getI18n()->format($this->tooShort, [
161 162
				'attribute' => $label,
				'min' => $this->min,
163
			], Yii::$app->language);
w  
Qiang Xue committed
164
		}
w  
Qiang Xue committed
165
		if ($this->max !== null) {
166
			$options['max'] = $this->max;
167
			$options['tooLong'] = Yii::$app->getI18n()->format($this->tooLong, [
168 169
				'attribute' => $label,
				'max' => $this->max,
170
			], Yii::$app->language);
w  
Qiang Xue committed
171
		}
172 173
		if ($this->length !== null) {
			$options['is'] = $this->length;
174
			$options['notEqual'] = Yii::$app->getI18n()->format($this->notEqual, [
175 176
				'attribute' => $label,
				'length' => $this->length,
177
			], Yii::$app->language);
w  
Qiang Xue committed
178
		}
Qiang Xue committed
179
		if ($this->skipOnEmpty) {
180
			$options['skipOnEmpty'] = 1;
w  
Qiang Xue committed
181 182
		}

183
		ValidationAsset::register($view);
184
		return 'yii.validation.string(value, messages, ' . json_encode($options) . ');';
w  
Qiang Xue committed
185 186
	}
}