StringValidator.php 4.83 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 32 33 34 35
{
	/**
	 * @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
36 37 38 39 40
	 * @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
41 42 43
	 */
	public $tooShort;
	/**
w  
Qiang Xue committed
44
	 * @var string user-defined error message used when the length of the value is greater than [[max]].
w  
Qiang Xue committed
45 46
	 */
	public $tooLong;
w  
Qiang Xue committed
47 48 49 50
	/**
	 * @var string user-defined error message used when the length of the value is not equal to [[is]].
	 */
	public $notEqual;
w  
Qiang Xue committed
51
	/**
Qiang Xue committed
52 53
	 * @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
54
	 */
Qiang Xue committed
55 56 57
	public $encoding;


w  
Qiang Xue committed
58
	/**
Qiang Xue committed
59
	 * Initializes the validator.
w  
Qiang Xue committed
60
	 */
Qiang Xue committed
61 62 63 64 65 66
	public function init()
	{
		parent::init();
		if ($this->encoding === null) {
			$this->encoding = Yii::$app->charset;
		}
Qiang Xue committed
67 68 69 70 71 72 73 74 75 76 77 78
		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
79
	}
w  
Qiang Xue committed
80 81 82 83

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

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

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

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

Qiang Xue committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	/**
	 * 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
125 126
	/**
	 * Returns the JavaScript needed for performing client-side validation.
w  
Qiang Xue committed
127
	 * @param \yii\base\Model $object the data object being validated
w  
Qiang Xue committed
128 129 130 131 132 133
	 * @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
134
		$value = $object->$attribute;
w  
Qiang Xue committed
135

136
		$options = array(
137 138 139 140
			'message' => Html::encode(strtr($this->message, array(
				'{attribute}' => $label,
				'{value}' => $value,
			))),
141 142
		);

w  
Qiang Xue committed
143
		if ($this->min !== null) {
144
			$options['min'] = $this->min;
145 146 147 148 149
			$options['tooShort'] = Html::encode(strtr($this->tooShort, array(
				'{attribute}' => $label,
				'{value}' => $value,
				'{min}' => $this->min,
			)));
w  
Qiang Xue committed
150
		}
w  
Qiang Xue committed
151
		if ($this->max !== null) {
152
			$options['max'] = $this->max;
153 154 155 156 157
			$options['tooLong'] = Html::encode(strtr($this->tooLong, array(
				'{attribute}' => $label,
				'{value}' => $value,
				'{max}' => $this->max,
			)));
w  
Qiang Xue committed
158
		}
w  
Qiang Xue committed
159
		if ($this->is !== null) {
160
			$options['is'] = $this->is;
161 162 163 164 165
			$options['notEqual'] = Html::encode(strtr($this->notEqual, array(
				'{attribute}' => $label,
				'{value}' => $value,
				'{length}' => $this->is,
			)));
w  
Qiang Xue committed
166
		}
Qiang Xue committed
167
		if ($this->skipOnEmpty) {
168
			$options['skipOnEmpty'] = 1;
w  
Qiang Xue committed
169 170
		}

171
		return 'yii.validation.string(value, messages, ' . json_encode($options) . ');';
w  
Qiang Xue committed
172 173 174
	}
}