RegularExpressionValidator.php 2.43 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;
Qiang Xue committed
12
use yii\web\JsExpression;
13
use yii\helpers\Json;
Qiang Xue committed
14

w  
Qiang Xue committed
15
/**
w  
Qiang Xue committed
16 17
 * RegularExpressionValidator validates that the attribute value matches the specified [[pattern]].
 *
w  
Qiang Xue committed
18
 * If the [[not]] property is set true, the validator will ensure the attribute value do NOT match the [[pattern]].
w  
Qiang Xue committed
19 20
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
21
 * @since 2.0
w  
Qiang Xue committed
22
 */
w  
Qiang Xue committed
23
class RegularExpressionValidator extends Validator
w  
Qiang Xue committed
24 25 26 27 28 29 30
{
	/**
	 * @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
31
	 * the regular expression defined via [[pattern]] should NOT match the attribute value.
w  
Qiang Xue committed
32
	 **/
resurtm committed
33
	public $not = false;
w  
Qiang Xue committed
34

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

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

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

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

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