1: <?php
2: /**
3: * CRegularExpressionValidator class file.
4: *
5: * @author Qiang Xue <qiang.xue@gmail.com>
6: * @link http://www.yiiframework.com/
7: * @copyright 2008-2013 Yii Software LLC
8: * @license http://www.yiiframework.com/license/
9: */
10:
11: /**
12: * CRegularExpressionValidator validates that the attribute value matches to the specified {@link pattern regular expression}.
13: * You may invert the validation logic with help of the {@link not} property (available since 1.1.5).
14: *
15: * @author Qiang Xue <qiang.xue@gmail.com>
16: * @package system.validators
17: * @since 1.0
18: */
19: class CRegularExpressionValidator extends CValidator
20: {
21: /**
22: * @var string the regular expression to be matched with
23: */
24: public $pattern;
25: /**
26: * @var boolean whether the attribute value can be null or empty. Defaults to true,
27: * meaning that if the attribute is empty, it is considered valid.
28: */
29: public $allowEmpty=true;
30: /**
31: * @var boolean whether to invert the validation logic. Defaults to false. If set to true,
32: * the regular expression defined via {@link pattern} should NOT match the attribute value.
33: * @since 1.1.5
34: **/
35: public $not=false;
36:
37: /**
38: * Validates the attribute of the object.
39: * If there is any error, the error message is added to the object.
40: * @param CModel $object the object being validated
41: * @param string $attribute the attribute being validated
42: * @throws CException if given {@link pattern} is empty
43: */
44: protected function validateAttribute($object,$attribute)
45: {
46: $value=$object->$attribute;
47: if($this->allowEmpty && $this->isEmpty($value))
48: return;
49: if($this->pattern===null)
50: throw new CException(Yii::t('yii','The "pattern" property must be specified with a valid regular expression.'));
51: // reason of array checking explained here: https://github.com/yiisoft/yii/issues/1955
52: if(is_array($value) ||
53: (!$this->not && !preg_match($this->pattern,$value)) ||
54: ($this->not && preg_match($this->pattern,$value)))
55: {
56: $message=$this->message!==null?$this->message:Yii::t('yii','{attribute} is invalid.');
57: $this->addError($object,$attribute,$message);
58: }
59: }
60:
61: /**
62: * Returns the JavaScript needed for performing client-side validation.
63: * @param CModel $object the data object being validated
64: * @param string $attribute the name of the attribute to be validated.
65: * @throws CException if given {@link pattern} is empty
66: * @return string the client-side validation script.
67: * @see CActiveForm::enableClientValidation
68: * @since 1.1.7
69: */
70: public function clientValidateAttribute($object,$attribute)
71: {
72: if($this->pattern===null)
73: throw new CException(Yii::t('yii','The "pattern" property must be specified with a valid regular expression.'));
74:
75: $message=$this->message!==null ? $this->message : Yii::t('yii','{attribute} is invalid.');
76: $message=strtr($message, array(
77: '{attribute}'=>$object->getAttributeLabel($attribute),
78: ));
79:
80: $pattern=$this->pattern;
81: $pattern=preg_replace('/\\\\x\{?([0-9a-fA-F]+)\}?/', '\u$1', $pattern);
82: $delim=substr($pattern, 0, 1);
83: $endpos=strrpos($pattern, $delim, 1);
84: $flag=substr($pattern, $endpos + 1);
85: if ($delim!=='/')
86: $pattern='/' . str_replace('/', '\\/', substr($pattern, 1, $endpos - 1)) . '/';
87: else
88: $pattern = substr($pattern, 0, $endpos + 1);
89: if (!empty($flag))
90: $pattern .= preg_replace('/[^igm]/', '', $flag);
91:
92: return "
93: if(".($this->allowEmpty ? "jQuery.trim(value)!='' && " : '').($this->not ? '' : '!')."value.match($pattern)) {
94: messages.push(".CJSON::encode($message).");
95: }
96: ";
97: }
98: }