RegularExpressionValidator.php 3.36 KB
Newer Older
w  
Qiang Xue committed
1 2
<?php
/**
w  
Qiang Xue committed
3
 * RegularExpressionValidator class file.
w  
Qiang Xue committed
4 5
 *
 * @link http://www.yiiframework.com/
Qiang Xue committed
6
 * @copyright Copyright &copy; 2008 Yii Software LLC
w  
Qiang Xue committed
7 8 9
 * @license http://www.yiiframework.com/license/
 */

w  
Qiang Xue committed
10 11
namespace yii\validators;

w  
Qiang Xue committed
12
/**
w  
Qiang Xue committed
13 14
 * RegularExpressionValidator validates that the attribute value matches the specified [[pattern]].
 *
w  
Qiang Xue committed
15
 * If the [[not]] property is set true, the validator will ensure the attribute value do NOT match the [[pattern]].
w  
Qiang Xue committed
16 17
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
18
 * @since 2.0
w  
Qiang Xue committed
19
 */
w  
Qiang Xue committed
20
class RegularExpressionValidator extends Validator
w  
Qiang Xue committed
21 22 23 24 25 26 27 28 29 30 31 32
{
	/**
	 * @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
33
	 * the regular expression defined via [[pattern]] should NOT match the attribute value.
w  
Qiang Xue committed
34 35 36 37 38 39
	 **/
 	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
40
	 * @param \yii\base\Model $object the object being validated
w  
Qiang Xue committed
41
	 * @param string $attribute the attribute being validated
Alexander Makarov committed
42
	 * @throws \yii\base\Exception if the "pattern" is not a valid regular expression
w  
Qiang Xue committed
43
	 */
w  
Qiang Xue committed
44
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
45 46
	{
		$value = $object->$attribute;
w  
Qiang Xue committed
47
		if ($this->allowEmpty && $this->isEmpty($value)) {
w  
Qiang Xue committed
48
			return;
w  
Qiang Xue committed
49 50
		}
		if ($this->pattern === null) {
Alexander Makarov committed
51
			throw new \yii\base\Exception('The "pattern" property must be specified with a valid regular expression.');
w  
Qiang Xue committed
52 53
		}
		if ((!$this->not && !preg_match($this->pattern, $value)) || ($this->not && preg_match($this->pattern, $value))) {
54
			$message = ($this->message !== null) ? $this->message : \Yii::t('yii|{attribute} is invalid.');
w  
Qiang Xue committed
55 56 57 58 59 60
			$this->addError($object, $attribute, $message);
		}
	}

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

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

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

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