StringValidator.php 5.72 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 28 29 30 31
	 * @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;
	 * - an array of one element: the minimum length that the value should be of. For example, `array(8)`.
	 *   This will overwrite [[min]].
	 * - an array of two elements: the minimum and maximum lengths that the value should be of.
	 *   For example, `array(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
	 * Initializes the validator.
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 98 99

	/**
	 * Validates the attribute of the object.
	 * If there is any error, the error message is added to the object.
w  
Qiang Xue committed
100
	 * @param \yii\base\Model $object the object being validated
w  
Qiang Xue committed
101 102
	 * @param string $attribute the attribute being validated
	 */
w  
Qiang Xue committed
103
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
104 105
	{
		$value = $object->$attribute;
w  
Qiang Xue committed
106 107

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

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

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

Qiang Xue committed
125 126 127 128 129 130 131 132 133 134 135 136 137
	/**
	 * Validates the given value.
	 * @param mixed $value the value to be validated.
	 * @return boolean whether the value is valid.
	 */
	public function validateValue($value)
	{
		if (!is_string($value)) {
			return false;
		}
		$length = mb_strlen($value, $this->encoding);
		return ($this->min === null || $length >= $this->min)
			&& ($this->max === null || $length <= $this->max)
138
			&& ($this->length === null || $length === $this->length);
Qiang Xue committed
139 140
	}

w  
Qiang Xue committed
141 142
	/**
	 * Returns the JavaScript needed for performing client-side validation.
w  
Qiang Xue committed
143
	 * @param \yii\base\Model $object the data object being validated
w  
Qiang Xue committed
144
	 * @param string $attribute the name of the attribute to be validated.
145 146
	 * @param \yii\base\View $view the view object that is going to be used to render views or view files
	 * containing a model form with this validator applied.
w  
Qiang Xue committed
147 148
	 * @return string the client-side validation script.
	 */
149
	public function clientValidateAttribute($object, $attribute, $view)
w  
Qiang Xue committed
150 151
	{
		$label = $object->getAttributeLabel($attribute);
w  
Qiang Xue committed
152
		$value = $object->$attribute;
w  
Qiang Xue committed
153

154
		$options = array(
155 156 157 158
			'message' => Html::encode(strtr($this->message, array(
				'{attribute}' => $label,
				'{value}' => $value,
			))),
159 160
		);

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

189
		ValidationAsset::register($view);
190
		return 'yii.validation.string(value, messages, ' . json_encode($options) . ');';
w  
Qiang Xue committed
191 192
	}
}