StringValidator.php 5 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
use Yii;
11
use yii\helpers\Html;
Qiang Xue committed
12

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


w  
Qiang Xue committed
65
	/**
Qiang Xue committed
66
	 * @inheritdoc
w  
Qiang Xue committed
67
	 */
Qiang Xue committed
68 69 70
	public function init()
	{
		parent::init();
71 72 73 74 75 76 77 78 79
		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
80 81 82
		if ($this->encoding === null) {
			$this->encoding = Yii::$app->charset;
		}
Qiang Xue committed
83
		if ($this->message === null) {
84
			$this->message = Yii::t('yii', '{attribute} must be a string.');
Qiang Xue committed
85 86
		}
		if ($this->min !== null && $this->tooShort === null) {
87
			$this->tooShort = Yii::t('yii', '{attribute} should contain at least {min} characters.');
Qiang Xue committed
88 89
		}
		if ($this->max !== null && $this->tooLong === null) {
90
			$this->tooLong = Yii::t('yii', '{attribute} should contain at most {max} characters.');
Qiang Xue committed
91
		}
92
		if ($this->length !== null && $this->notEqual === null) {
93
			$this->notEqual = Yii::t('yii', '{attribute} should contain {length} characters.');
Qiang Xue committed
94
		}
Qiang Xue committed
95
	}
w  
Qiang Xue committed
96 97

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

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

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

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

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

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

		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
144 145
	}

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

Alexander Makarov committed
153 154
		$options = [
			'message' => Html::encode(strtr($this->message, [
155
				'{attribute}' => $label,
Alexander Makarov committed
156 157
			])),
		];
158

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

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