StringValidator.php 5.16 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/
w  
Qiang Xue committed
6
 * @copyright Copyright &copy; 2008-2012 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 79 80
		if ($this->allowEmpty && $this->isEmpty($value)) {
			return;
		}

		if (!is_string($value)) {
			$message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be a string.');
			$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) {
w  
Qiang Xue committed
85
			$length = mb_strlen($value, $this->encoding ? $this->encoding : Yii::app()->charset);
w  
Qiang Xue committed
86 87
		}
		else {
w  
Qiang Xue committed
88
			$length = strlen($value);
w  
Qiang Xue committed
89
		}
w  
Qiang Xue committed
90

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

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

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

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

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

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

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

		return $js;
	}
}