RegularExpressionValidator.php 2.41 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;
Qiang Xue committed
11
use yii\base\InvalidConfigException;
12
use yii\helpers\Html;
Qiang Xue committed
13
use yii\web\JsExpression;
14
use yii\helpers\Json;
Qiang Xue committed
15

w  
Qiang Xue committed
16
/**
w  
Qiang Xue committed
17 18
 * RegularExpressionValidator validates that the attribute value matches the specified [[pattern]].
 *
w  
Qiang Xue committed
19
 * If the [[not]] property is set true, the validator will ensure the attribute value do NOT match the [[pattern]].
w  
Qiang Xue committed
20 21
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
22
 * @since 2.0
w  
Qiang Xue committed
23
 */
w  
Qiang Xue committed
24
class RegularExpressionValidator extends Validator
w  
Qiang Xue committed
25 26 27 28 29 30 31
{
	/**
	 * @var string the regular expression to be matched with
	 */
	public $pattern;
	/**
	 * @var boolean whether to invert the validation logic. Defaults to false. If set to true,
w  
Qiang Xue committed
32
	 * the regular expression defined via [[pattern]] should NOT match the attribute value.
w  
Qiang Xue committed
33
	 **/
resurtm committed
34
	public $not = false;
w  
Qiang Xue committed
35

Qiang Xue committed
36
	/**
Qiang Xue committed
37
	 * @inheritdoc
Qiang Xue committed
38 39 40 41 42 43 44
	 */
	public function init()
	{
		parent::init();
		if ($this->pattern === null) {
			throw new InvalidConfigException('The "pattern" property must be set.');
		}
Qiang Xue committed
45
		if ($this->message === null) {
46
			$this->message = Yii::t('yii', '{attribute} is invalid.');
Qiang Xue committed
47
		}
Qiang Xue committed
48 49
	}

w  
Qiang Xue committed
50
	/**
Qiang Xue committed
51
	 * @inheritdoc
w  
Qiang Xue committed
52
	 */
Qiang Xue committed
53
	protected function validateValue($value)
w  
Qiang Xue committed
54
	{
Qiang Xue committed
55
		$valid = !is_array($value) &&
56 57
			(!$this->not && preg_match($this->pattern, $value)
			|| $this->not && !preg_match($this->pattern, $value));
Qiang Xue committed
58
		return $valid ? null : [$this->message, []];
Qiang Xue committed
59 60
	}

w  
Qiang Xue committed
61
	/**
Qiang Xue committed
62
	 * @inheritdoc
w  
Qiang Xue committed
63
	 */
64
	public function clientValidateAttribute($object, $attribute, $view)
w  
Qiang Xue committed
65 66 67
	{
		$pattern = $this->pattern;
		$pattern = preg_replace('/\\\\x\{?([0-9a-fA-F]+)\}?/', '\u$1', $pattern);
Qiang Xue committed
68 69 70 71 72
		$deliminator = substr($pattern, 0, 1);
		$pos = strrpos($pattern, $deliminator, 1);
		$flag = substr($pattern, $pos + 1);
		if ($deliminator !== '/') {
			$pattern = '/' . str_replace('/', '\\/', substr($pattern, 1, $pos - 1)) . '/';
Qiang Xue committed
73
		} else {
Qiang Xue committed
74
			$pattern = substr($pattern, 0, $pos + 1);
w  
Qiang Xue committed
75 76
		}
		if (!empty($flag)) {
w  
Qiang Xue committed
77
			$pattern .= preg_replace('/[^igm]/', '', $flag);
w  
Qiang Xue committed
78
		}
w  
Qiang Xue committed
79

Alexander Makarov committed
80
		$options = [
81
			'pattern' => new JsExpression($pattern),
82
			'not' => $this->not,
83
			'message' => strtr($this->message, [
84
				'{attribute}' => $object->getAttributeLabel($attribute),
85
			]),
Alexander Makarov committed
86
		];
87 88 89 90
		if ($this->skipOnEmpty) {
			$options['skipOnEmpty'] = 1;
		}

91
		ValidationAsset::register($view);
92
		return 'yii.validation.regularExpression(value, messages, ' . Json::encode($options) . ');';
w  
Qiang Xue committed
93
	}
w  
Qiang Xue committed
94
}