RegularExpressionValidator.php 3.6 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
	 **/
resurtm committed
35
	public $not = false;
w  
Qiang Xue committed
36

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
		if ($this->message === null) {
48
			$this->message = Yii::t('yii', '{attribute} is invalid.');
Qiang Xue committed
49
		}
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
	 * @param string $attribute the name of the attribute to be validated.
82 83
	 * @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
84
	 * @return string the client-side validation script.
Qiang Xue committed
85
	 * @throws InvalidConfigException if the "pattern" is not a valid regular expression
w  
Qiang Xue committed
86
	 */
87
	public function clientValidateAttribute($object, $attribute, $view)
w  
Qiang Xue committed
88 89 90
	{
		$pattern = $this->pattern;
		$pattern = preg_replace('/\\\\x\{?([0-9a-fA-F]+)\}?/', '\u$1', $pattern);
Qiang Xue committed
91 92 93 94 95
		$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
96
		} else {
Qiang Xue committed
97
			$pattern = substr($pattern, 0, $pos + 1);
w  
Qiang Xue committed
98 99
		}
		if (!empty($flag)) {
w  
Qiang Xue committed
100
			$pattern .= preg_replace('/[^igm]/', '', $flag);
w  
Qiang Xue committed
101
		}
w  
Qiang Xue committed
102

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

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