StringValidator.php 5.05 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
		if ($this->message === null) {
68
			$this->message = Yii::t('yii', '{attribute} must be a string.');
Qiang Xue committed
69 70
		}
		if ($this->min !== null && $this->tooShort === null) {
71
			$this->tooShort = Yii::t('yii', '{attribute} should contain at least {min} characters.');
Qiang Xue committed
72 73
		}
		if ($this->max !== null && $this->tooLong === null) {
74
			$this->tooLong = Yii::t('yii', '{attribute} should contain at most {max} characters.');
Qiang Xue committed
75 76
		}
		if ($this->is !== null && $this->notEqual === null) {
77
			$this->notEqual = Yii::t('yii', '{attribute} should contain {length} characters.');
Qiang Xue committed
78
		}
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
	 * @param string $attribute the name of the attribute to be validated.
129 130
	 * @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
131 132
	 * @return string the client-side validation script.
	 */
133
	public function clientValidateAttribute($object, $attribute, $view)
w  
Qiang Xue committed
134 135
	{
		$label = $object->getAttributeLabel($attribute);
w  
Qiang Xue committed
136
		$value = $object->$attribute;
w  
Qiang Xue committed
137

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

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

173
		$view->registerAssetBundle('yii/validation');
174
		return 'yii.validation.string(value, messages, ' . json_encode($options) . ');';
w  
Qiang Xue committed
175 176 177
	}
}