StringValidator.php 4.73 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 27 28 29 30 31 32 33 34
{
	/**
	 * @var integer maximum length. Defaults to null, meaning no maximum limit.
	 */
	public $max;
	/**
	 * @var integer minimum length. Defaults to null, meaning no minimum limit.
	 */
	public $min;
	/**
	 * @var integer exact length. Defaults to null, meaning no exact length limit.
	 */
	public $is;
	/**
w  
Qiang Xue committed
35 36 37 38 39
	 * @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
40 41 42
	 */
	public $tooShort;
	/**
w  
Qiang Xue committed
43
	 * @var string user-defined error message used when the length of the value is greater than [[max]].
w  
Qiang Xue committed
44 45
	 */
	public $tooLong;
w  
Qiang Xue committed
46 47 48 49
	/**
	 * @var string user-defined error message used when the length of the value is not equal to [[is]].
	 */
	public $notEqual;
w  
Qiang Xue committed
50
	/**
Qiang Xue committed
51 52
	 * @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
53
	 */
Qiang Xue committed
54 55 56
	public $encoding;


w  
Qiang Xue committed
57
	/**
Qiang Xue committed
58
	 * Initializes the validator.
w  
Qiang Xue committed
59
	 */
Qiang Xue committed
60 61 62 63 64 65
	public function init()
	{
		parent::init();
		if ($this->encoding === null) {
			$this->encoding = Yii::$app->charset;
		}
Qiang Xue committed
66 67 68 69 70 71 72 73 74 75 76 77
		if ($this->message === null) {
			$this->message = Yii::t('yii|{attribute} must be a string.');
		}
		if ($this->min !== null && $this->tooShort === null) {
			$this->tooShort = Yii::t('yii|{attribute} should contain at least {min} characters.');
		}
		if ($this->max !== null && $this->tooLong === null) {
			$this->tooLong = Yii::t('yii|{attribute} should contain at most {max} characters.');
		}
		if ($this->is !== null && $this->notEqual === null) {
			$this->notEqual = Yii::t('yii|{attribute} should contain {length} characters.');
		}
Qiang Xue committed
78
	}
w  
Qiang Xue committed
79 80 81 82

	/**
	 * Validates the attribute of the object.
	 * If there is any error, the error message is added to the object.
w  
Qiang Xue committed
83
	 * @param \yii\base\Model $object the object being validated
w  
Qiang Xue committed
84 85
	 * @param string $attribute the attribute being validated
	 */
w  
Qiang Xue committed
86
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
87 88
	{
		$value = $object->$attribute;
w  
Qiang Xue committed
89 90

		if (!is_string($value)) {
Qiang Xue committed
91
			$this->addError($object, $attribute, $this->message);
w  
Qiang Xue committed
92
			return;
w  
Qiang Xue committed
93
		}
w  
Qiang Xue committed
94

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

w  
Qiang Xue committed
97
		if ($this->min !== null && $length < $this->min) {
Qiang Xue committed
98
			$this->addError($object, $attribute, $this->tooShort, array('{min}' => $this->min));
w  
Qiang Xue committed
99
		}
w  
Qiang Xue committed
100
		if ($this->max !== null && $length > $this->max) {
Qiang Xue committed
101
			$this->addError($object, $attribute, $this->tooLong, array('{max}' => $this->max));
w  
Qiang Xue committed
102
		}
w  
Qiang Xue committed
103
		if ($this->is !== null && $length !== $this->is) {
Qiang Xue committed
104
			$this->addError($object, $attribute, $this->notEqual, array('{length}' => $this->is));
w  
Qiang Xue committed
105 106 107
		}
	}

Qiang Xue committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
	/**
	 * 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)
			&& ($this->is === null || $length === $this->is);
	}

w  
Qiang Xue committed
124 125
	/**
	 * Returns the JavaScript needed for performing client-side validation.
w  
Qiang Xue committed
126
	 * @param \yii\base\Model $object the data object being validated
w  
Qiang Xue committed
127 128 129 130 131 132
	 * @param string $attribute the name of the attribute to be validated.
	 * @return string the client-side validation script.
	 */
	public function clientValidateAttribute($object, $attribute)
	{
		$label = $object->getAttributeLabel($attribute);
w  
Qiang Xue committed
133
		$value = $object->$attribute;
w  
Qiang Xue committed
134

Qiang Xue committed
135
		$notEqual = strtr($this->notEqual, array(
w  
Qiang Xue committed
136
			'{attribute}' => $label,
w  
Qiang Xue committed
137
			'{value}' => $value,
w  
Qiang Xue committed
138 139 140
			'{length}' => $this->is,
		));

Qiang Xue committed
141
		$tooShort = strtr($this->tooShort, array(
w  
Qiang Xue committed
142
			'{attribute}' => $label,
w  
Qiang Xue committed
143
			'{value}' => $value,
w  
Qiang Xue committed
144 145 146
			'{min}' => $this->min,
		));

Qiang Xue committed
147
		$tooLong = strtr($this->tooLong, array(
w  
Qiang Xue committed
148
			'{attribute}' => $label,
w  
Qiang Xue committed
149
			'{value}' => $value,
w  
Qiang Xue committed
150 151 152 153
			'{max}' => $this->max,
		));

		$js = '';
w  
Qiang Xue committed
154
		if ($this->min !== null) {
w  
Qiang Xue committed
155 156
			$js .= "
if(value.length< {$this->min}) {
w  
Qiang Xue committed
157
	messages.push(" . json_encode($tooShort) . ");
w  
Qiang Xue committed
158 159 160
}
";
		}
w  
Qiang Xue committed
161
		if ($this->max !== null) {
w  
Qiang Xue committed
162 163
			$js .= "
if(value.length> {$this->max}) {
w  
Qiang Xue committed
164
	messages.push(" . json_encode($tooLong) . ");
w  
Qiang Xue committed
165 166 167
}
";
		}
w  
Qiang Xue committed
168
		if ($this->is !== null) {
w  
Qiang Xue committed
169 170
			$js .= "
if(value.length!= {$this->is}) {
w  
Qiang Xue committed
171
	messages.push(" . json_encode($notEqual) . ");
w  
Qiang Xue committed
172 173 174 175
}
";
		}

Qiang Xue committed
176
		if ($this->skipOnEmpty) {
w  
Qiang Xue committed
177 178 179 180 181 182 183 184 185 186 187
			$js = "
if($.trim(value)!='') {
	$js
}
";
		}

		return $js;
	}
}