Overview

Packages

  • application
    • commands
    • components
      • actions
      • filters
      • leftWidget
      • permissions
      • sortableWidget
      • util
      • webupdater
      • x2flow
        • actions
        • triggers
      • X2GridView
      • X2Settings
    • controllers
    • models
      • embedded
    • modules
      • accounts
        • controllers
        • models
      • actions
        • controllers
        • models
      • calendar
        • controllers
        • models
      • charts
        • models
      • contacts
        • controllers
        • models
      • docs
        • components
        • controllers
        • models
      • groups
        • controllers
        • models
      • marketing
        • components
        • controllers
        • models
      • media
        • controllers
        • models
      • mobile
        • components
      • opportunities
        • controllers
        • models
      • products
        • controllers
        • models
      • quotes
        • controllers
        • models
      • services
        • controllers
        • models
      • template
        • models
      • users
        • controllers
        • models
      • workflow
        • controllers
        • models
      • x2Leads
        • controllers
        • models
  • Net
  • None
  • PHP
  • system
    • base
    • caching
      • dependencies
    • collections
    • console
    • db
      • ar
      • schema
        • cubrid
        • mssql
        • mysql
        • oci
        • pgsql
        • sqlite
    • i18n
      • gettext
    • logging
    • test
    • utils
    • validators
    • web
      • actions
      • auth
      • filters
      • form
      • helpers
      • renderers
      • services
      • widgets
        • captcha
        • pagers
  • Text
    • Highlighter
  • zii
    • behaviors
    • widgets
      • grid
      • jui

Classes

  • CBooleanValidator
  • CCaptchaValidator
  • CCompareValidator
  • CDateValidator
  • CDefaultValueValidator
  • CEmailValidator
  • CExistValidator
  • CFileValidator
  • CFilterValidator
  • CInlineValidator
  • CNumberValidator
  • CRangeValidator
  • CRegularExpressionValidator
  • CRequiredValidator
  • CSafeValidator
  • CStringValidator
  • CTypeValidator
  • CUniqueValidator
  • CUnsafeValidator
  • CUrlValidator
  • CValidator
  • X2UrlValidator
  • Overview
  • Package
  • Class
  • Tree
 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: }
API documentation generated by ApiGen 2.8.0