StringValidator.php 5.17 KB
Newer Older
w  
Qiang Xue committed
1 2
<?php
/**
w  
Qiang Xue committed
3
 * StringValidator class file.
w  
Qiang Xue committed
4 5
 *
 * @link http://www.yiiframework.com/
Qiang Xue committed
6
 * @copyright Copyright &copy; 2008 Yii Software LLC
w  
Qiang Xue committed
7 8 9
 * @license http://www.yiiframework.com/license/
 */

w  
Qiang Xue committed
10 11
namespace yii\validators;

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 51 52 53 54 55
	/**
	 * @var boolean whether the attribute value can be null or empty. Defaults to true,
	 * meaning that if the attribute is empty, it is considered valid.
	 */
	public $allowEmpty = true;
	/**
w  
Qiang Xue committed
56
	 * @var mixed the encoding of the string value to be validated (e.g. 'UTF-8').
w  
Qiang Xue committed
57 58 59
	 * This property is used only when mbstring PHP extension is enabled.
	 * The value of this property will be used as the 2nd parameter of the
	 * mb_strlen() function. If this property is not set, the application charset
w  
Qiang Xue committed
60 61
	 * will be used. If this property is set false, then strlen() will be used even
	 * if mbstring is enabled.
w  
Qiang Xue committed
62 63 64 65 66 67
	 */
	public $encoding;

	/**
	 * Validates the attribute of the object.
	 * If there is any error, the error message is added to the object.
w  
Qiang Xue committed
68
	 * @param \yii\base\Model $object the object being validated
w  
Qiang Xue committed
69 70
	 * @param string $attribute the attribute being validated
	 */
w  
Qiang Xue committed
71
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
72 73
	{
		$value = $object->$attribute;
w  
Qiang Xue committed
74 75 76 77 78
		if ($this->allowEmpty && $this->isEmpty($value)) {
			return;
		}

		if (!is_string($value)) {
Alexander Makarov committed
79
			$message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} must be a string.');
w  
Qiang Xue committed
80
			$this->addError($object, $attribute, $message);
w  
Qiang Xue committed
81
			return;
w  
Qiang Xue committed
82
		}
w  
Qiang Xue committed
83

w  
Qiang Xue committed
84
		if (function_exists('mb_strlen') && $this->encoding !== false) {
Qiang Xue committed
85
			$length = mb_strlen($value, $this->encoding ? $this->encoding : \Yii::$application->charset);
Qiang Xue committed
86
		} else {
w  
Qiang Xue committed
87
			$length = strlen($value);
w  
Qiang Xue committed
88
		}
w  
Qiang Xue committed
89

w  
Qiang Xue committed
90
		if ($this->min !== null && $length < $this->min) {
Alexander Makarov committed
91
			$message = ($this->tooShort !== null) ? $this->tooShort : \Yii::t('yii', '{attribute} is too short (minimum is {min} characters).');
w  
Qiang Xue committed
92 93
			$this->addError($object, $attribute, $message, array('{min}' => $this->min));
		}
w  
Qiang Xue committed
94
		if ($this->max !== null && $length > $this->max) {
Alexander Makarov committed
95
			$message = ($this->tooLong !== null) ? $this->tooLong : \Yii::t('yii', '{attribute} is too long (maximum is {max} characters).');
w  
Qiang Xue committed
96 97
			$this->addError($object, $attribute, $message, array('{max}' => $this->max));
		}
w  
Qiang Xue committed
98
		if ($this->is !== null && $length !== $this->is) {
Alexander Makarov committed
99
			$message = ($this->notEqual !== null) ? $this->notEqual : \Yii::t('yii', '{attribute} is of the wrong length (should be {length} characters).');
w  
Qiang Xue committed
100 101 102 103 104 105
			$this->addError($object, $attribute, $message, array('{length}' => $this->is));
		}
	}

	/**
	 * Returns the JavaScript needed for performing client-side validation.
w  
Qiang Xue committed
106
	 * @param \yii\base\Model $object the data object being validated
w  
Qiang Xue committed
107 108 109 110 111 112
	 * @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
113
		$value = $object->$attribute;
w  
Qiang Xue committed
114

w  
Qiang Xue committed
115
		if (($notEqual = $this->notEqual) === null) {
Alexander Makarov committed
116
			$notEqual = \Yii::t('yii', '{attribute} is of the wrong length (should be {length} characters).');
w  
Qiang Xue committed
117 118
		}
		$notEqual = strtr($notEqual, array(
w  
Qiang Xue committed
119
			'{attribute}' => $label,
w  
Qiang Xue committed
120
			'{value}' => $value,
w  
Qiang Xue committed
121 122 123
			'{length}' => $this->is,
		));

w  
Qiang Xue committed
124
		if (($tooShort = $this->tooShort) === null) {
Alexander Makarov committed
125
			$tooShort = \Yii::t('yii', '{attribute} is too short (minimum is {min} characters).');
w  
Qiang Xue committed
126
		}
w  
Qiang Xue committed
127 128
		$tooShort = strtr($tooShort, array(
			'{attribute}' => $label,
w  
Qiang Xue committed
129
			'{value}' => $value,
w  
Qiang Xue committed
130 131 132
			'{min}' => $this->min,
		));

w  
Qiang Xue committed
133
		if (($tooLong = $this->tooLong) === null) {
Alexander Makarov committed
134
			$tooLong = \Yii::t('yii', '{attribute} is too long (maximum is {max} characters).');
w  
Qiang Xue committed
135
		}
w  
Qiang Xue committed
136 137
		$tooLong = strtr($tooLong, array(
			'{attribute}' => $label,
w  
Qiang Xue committed
138
			'{value}' => $value,
w  
Qiang Xue committed
139 140 141 142
			'{max}' => $this->max,
		));

		$js = '';
w  
Qiang Xue committed
143
		if ($this->min !== null) {
w  
Qiang Xue committed
144 145
			$js .= "
if(value.length< {$this->min}) {
w  
Qiang Xue committed
146
	messages.push(" . json_encode($tooShort) . ");
w  
Qiang Xue committed
147 148 149
}
";
		}
w  
Qiang Xue committed
150
		if ($this->max !== null) {
w  
Qiang Xue committed
151 152
			$js .= "
if(value.length> {$this->max}) {
w  
Qiang Xue committed
153
	messages.push(" . json_encode($tooLong) . ");
w  
Qiang Xue committed
154 155 156
}
";
		}
w  
Qiang Xue committed
157
		if ($this->is !== null) {
w  
Qiang Xue committed
158 159
			$js .= "
if(value.length!= {$this->is}) {
w  
Qiang Xue committed
160
	messages.push(" . json_encode($notEqual) . ");
w  
Qiang Xue committed
161 162 163 164
}
";
		}

w  
Qiang Xue committed
165
		if ($this->allowEmpty) {
w  
Qiang Xue committed
166 167 168 169 170 171 172 173 174 175 176
			$js = "
if($.trim(value)!='') {
	$js
}
";
		}

		return $js;
	}
}