RegularExpressionValidator.php 3.39 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 37 38 39 40 41 42 43 44 45
	/**
	 * Initializes the validator.
	 * @throws InvalidConfigException if [[pattern]] is not set.
	 */
	public function init()
	{
		parent::init();
		if ($this->pattern === null) {
			throw new InvalidConfigException('The "pattern" property must be set.');
		}
Qiang Xue committed
46
		if ($this->message === null) {
47
			$this->message = Yii::t('yii', '{attribute} is invalid.');
Qiang Xue committed
48
		}
Qiang Xue committed
49 50
	}

w  
Qiang Xue committed
51 52 53
	/**
	 * Validates the attribute of the object.
	 * If there is any error, the error message is added to the object.
w  
Qiang Xue committed
54
	 * @param \yii\base\Model $object the object being validated
w  
Qiang Xue committed
55 56
	 * @param string $attribute the attribute being validated
	 */
w  
Qiang Xue committed
57
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
58 59
	{
		$value = $object->$attribute;
60
		if (!$this->validateValue($value)) {
Qiang Xue committed
61
			$this->addError($object, $attribute, $this->message);
w  
Qiang Xue committed
62 63 64
		}
	}

Qiang Xue committed
65 66 67 68 69 70 71
	/**
	 * Validates the given value.
	 * @param mixed $value the value to be validated.
	 * @return boolean whether the value is valid.
	 */
	public function validateValue($value)
	{
72 73 74
		return !is_array($value) &&
			(!$this->not && preg_match($this->pattern, $value)
			|| $this->not && !preg_match($this->pattern, $value));
Qiang Xue committed
75 76
	}

w  
Qiang Xue committed
77 78
	/**
	 * Returns the JavaScript needed for performing client-side validation.
w  
Qiang Xue committed
79
	 * @param \yii\base\Model $object the data object being validated
w  
Qiang Xue committed
80
	 * @param string $attribute the name of the attribute to be validated.
81 82
	 * @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
83 84
	 * @return string the client-side validation script.
	 */
85
	public function clientValidateAttribute($object, $attribute, $view)
w  
Qiang Xue committed
86 87 88
	{
		$pattern = $this->pattern;
		$pattern = preg_replace('/\\\\x\{?([0-9a-fA-F]+)\}?/', '\u$1', $pattern);
Qiang Xue committed
89 90 91 92 93
		$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
94
		} else {
Qiang Xue committed
95
			$pattern = substr($pattern, 0, $pos + 1);
w  
Qiang Xue committed
96 97
		}
		if (!empty($flag)) {
w  
Qiang Xue committed
98
			$pattern .= preg_replace('/[^igm]/', '', $flag);
w  
Qiang Xue committed
99
		}
w  
Qiang Xue committed
100

Alexander Makarov committed
101
		$options = [
102
			'pattern' => new JsExpression($pattern),
103
			'not' => $this->not,
Alexander Makarov committed
104
			'message' => Html::encode(strtr($this->message, [
105
				'{attribute}' => $object->getAttributeLabel($attribute),
Alexander Makarov committed
106 107
			])),
		];
108 109 110 111
		if ($this->skipOnEmpty) {
			$options['skipOnEmpty'] = 1;
		}

112
		ValidationAsset::register($view);
113
		return 'yii.validation.regularExpression(value, messages, ' . Json::encode($options) . ');';
w  
Qiang Xue committed
114
	}
w  
Qiang Xue committed
115
}