RegularExpressionValidator.php 3.31 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;

w  
Qiang Xue committed
10
/**
w  
Qiang Xue committed
11 12
 * RegularExpressionValidator validates that the attribute value matches the specified [[pattern]].
 *
w  
Qiang Xue committed
13
 * If the [[not]] property is set true, the validator will ensure the attribute value do NOT match the [[pattern]].
w  
Qiang Xue committed
14 15
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
16
 * @since 2.0
w  
Qiang Xue committed
17
 */
w  
Qiang Xue committed
18
class RegularExpressionValidator extends Validator
w  
Qiang Xue committed
19 20 21 22 23 24 25 26 27 28 29 30
{
	/**
	 * @var string the regular expression to be matched with
	 */
	public $pattern;
	/**
	 * @var boolean whether the attribute value can be null or empty. Defaults to true,
	 * meaning that if the attribute is empty, it is considered valid.
	 */
	public $allowEmpty = true;
	/**
	 * @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 33 34 35 36 37
	 **/
 	public $not = false;

	/**
	 * Validates the attribute of the object.
	 * If there is any error, the error message is added to the object.
w  
Qiang Xue committed
38
	 * @param \yii\base\Model $object the object being validated
w  
Qiang Xue committed
39
	 * @param string $attribute the attribute being validated
Alexander Makarov committed
40
	 * @throws \yii\base\Exception if the "pattern" is not a valid regular expression
w  
Qiang Xue committed
41
	 */
w  
Qiang Xue committed
42
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
43 44
	{
		$value = $object->$attribute;
w  
Qiang Xue committed
45
		if ($this->allowEmpty && $this->isEmpty($value)) {
w  
Qiang Xue committed
46
			return;
w  
Qiang Xue committed
47 48
		}
		if ($this->pattern === null) {
Alexander Makarov committed
49
			throw new \yii\base\Exception('The "pattern" property must be specified with a valid regular expression.');
w  
Qiang Xue committed
50 51
		}
		if ((!$this->not && !preg_match($this->pattern, $value)) || ($this->not && preg_match($this->pattern, $value))) {
52
			$message = ($this->message !== null) ? $this->message : \Yii::t('yii|{attribute} is invalid.');
w  
Qiang Xue committed
53 54 55 56 57 58
			$this->addError($object, $attribute, $message);
		}
	}

	/**
	 * Returns the JavaScript needed for performing client-side validation.
w  
Qiang Xue committed
59
	 * @param \yii\base\Model $object the data object being validated
w  
Qiang Xue committed
60 61
	 * @param string $attribute the name of the attribute to be validated.
	 * @return string the client-side validation script.
Alexander Makarov committed
62
	 * @throws \yii\base\Exception if the "pattern" is not a valid regular expression
w  
Qiang Xue committed
63 64 65
	 */
	public function clientValidateAttribute($object, $attribute)
	{
w  
Qiang Xue committed
66
		if ($this->pattern === null) {
w  
Qiang Xue committed
67
			throw new \yii\base\Exception('The "pattern" property must be specified with a valid regular expression.');
w  
Qiang Xue committed
68
		}
w  
Qiang Xue committed
69

70
		$message = ($this->message !== null) ? $this->message : \Yii::t('yii|{attribute} is invalid.');
w  
Qiang Xue committed
71 72
		$message = strtr($message, array(
			'{attribute}' => $object->getAttributeLabel($attribute),
w  
Qiang Xue committed
73
			'{value}' => $object->$attribute,
w  
Qiang Xue committed
74 75 76 77
		));

		$pattern = $this->pattern;
		$pattern = preg_replace('/\\\\x\{?([0-9a-fA-F]+)\}?/', '\u$1', $pattern);
Qiang Xue committed
78 79 80 81 82
		$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
83
		} else {
Qiang Xue committed
84
			$pattern = substr($pattern, 0, $pos + 1);
w  
Qiang Xue committed
85 86
		}
		if (!empty($flag)) {
w  
Qiang Xue committed
87
			$pattern .= preg_replace('/[^igm]/', '', $flag);
w  
Qiang Xue committed
88
		}
w  
Qiang Xue committed
89 90

		return "
w  
Qiang Xue committed
91
if (" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . ($this->not ? '' : '!') . "value.match($pattern)) {
w  
Qiang Xue committed
92
	messages.push(" . json_encode($message) . ");
w  
Qiang Xue committed
93 94 95
}
";
	}
w  
Qiang Xue committed
96
}