RegularExpressionValidator.php 3.4 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.
Qiang Xue committed
33
	 * @throws InvalidConfigException if the "pattern" is not a valid regular expression
w  
Qiang Xue committed
34 35 36
	 **/
 	public $not = false;

Qiang Xue committed
37 38 39 40 41 42 43 44 45 46
	/**
	 * 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
47 48 49
		if ($this->message === null) {
			$this->message = Yii::t('yii|{attribute} is invalid.');
		}
Qiang Xue committed
50 51
	}

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

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

w  
Qiang Xue committed
78 79
	/**
	 * Returns the JavaScript needed for performing client-side validation.
w  
Qiang Xue committed
80
	 * @param \yii\base\Model $object the data object being validated
w  
Qiang Xue committed
81 82
	 * @param string $attribute the name of the attribute to be validated.
	 * @return string the client-side validation script.
Qiang Xue committed
83
	 * @throws InvalidConfigException if the "pattern" is not a valid regular expression
w  
Qiang Xue committed
84 85 86 87 88
	 */
	public function clientValidateAttribute($object, $attribute)
	{
		$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

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

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