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:  * CStringValidator 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:  * CStringValidator validates that the attribute value is of certain length.
 13:  *
 14:  * Note, this validator should only be used with string-typed attributes.
 15:  *
 16:  * In addition to the {@link message} property for setting a custom error message,
 17:  * CStringValidator has a couple custom error messages you can set that correspond to different
 18:  * validation scenarios. For defining a custom message when the string is too short,
 19:  * you may use the {@link tooShort} property. Similarly with {@link tooLong}. The messages may contain
 20:  * placeholders that will be replaced with the actual content. In addition to the "{attribute}"
 21:  * placeholder, recognized by all validators (see {@link CValidator}), CStringValidator allows for the following
 22:  * placeholders to be specified:
 23:  * <ul>
 24:  * <li>{min}: when using {@link tooShort}, replaced with minimum length, {@link min}, if set.</li>
 25:  * <li>{max}: when using {@link tooLong}, replaced with the maximum length, {@link max}, if set.</li>
 26:  * <li>{length}: when using {@link message}, replaced with the exact required length, {@link is}, if set.</li>
 27:  * </ul>
 28:  *
 29:  * @author Qiang Xue <qiang.xue@gmail.com>
 30:  * @package system.validators
 31:  * @since 1.0
 32:  */
 33: class CStringValidator extends CValidator
 34: {
 35:     /**
 36:      * @var integer maximum length. Defaults to null, meaning no maximum limit.
 37:      */
 38:     public $max;
 39:     /**
 40:      * @var integer minimum length. Defaults to null, meaning no minimum limit.
 41:      */
 42:     public $min;
 43:     /**
 44:      * @var integer exact length. Defaults to null, meaning no exact length limit.
 45:      */
 46:     public $is;
 47:     /**
 48:      * @var string user-defined error message used when the value is too short.
 49:      */
 50:     public $tooShort;
 51:     /**
 52:      * @var string user-defined error message used when the value is too long.
 53:      */
 54:     public $tooLong;
 55:     /**
 56:      * @var boolean whether the attribute value can be null or empty. Defaults to true,
 57:      * meaning that if the attribute is empty, it is considered valid.
 58:      */
 59:     public $allowEmpty=true;
 60:     /**
 61:      * @var string the encoding of the string value to be validated (e.g. 'UTF-8').
 62:      * This property is used only when mbstring PHP extension is enabled.
 63:      * The value of this property will be used as the 2nd parameter of the
 64:      * mb_strlen() function. If this property is not set, the application charset
 65:      * will be used.
 66:      * If this property is set false, then strlen() will be used even if mbstring is enabled.
 67:      * @since 1.1.1
 68:      */
 69:     public $encoding;
 70: 
 71:     /**
 72:      * Validates the attribute of the object.
 73:      * If there is any error, the error message is added to the object.
 74:      * @param CModel $object the object being validated
 75:      * @param string $attribute the attribute being validated
 76:      */
 77:     protected function validateAttribute($object,$attribute)
 78:     {
 79:         $value=$object->$attribute;
 80:         if($this->allowEmpty && $this->isEmpty($value))
 81:             return;
 82: 
 83:         if(is_array($value))
 84:         {
 85:             // https://github.com/yiisoft/yii/issues/1955
 86:             $this->addError($object,$attribute,Yii::t('yii','{attribute} is invalid.'));
 87:             return;
 88:         }
 89: 
 90:         if(function_exists('mb_strlen') && $this->encoding!==false)
 91:             $length=mb_strlen($value, $this->encoding ? $this->encoding : Yii::app()->charset);
 92:         else
 93:             $length=strlen($value);
 94: 
 95:         if($this->min!==null && $length<$this->min)
 96:         {
 97:             $message=$this->tooShort!==null?$this->tooShort:Yii::t('yii','{attribute} is too short (minimum is {min} characters).');
 98:             $this->addError($object,$attribute,$message,array('{min}'=>$this->min));
 99:         }
100:         if($this->max!==null && $length>$this->max)
101:         {
102:             $message=$this->tooLong!==null?$this->tooLong:Yii::t('yii','{attribute} is too long (maximum is {max} characters).');
103:             $this->addError($object,$attribute,$message,array('{max}'=>$this->max));
104:         }
105:         if($this->is!==null && $length!==$this->is)
106:         {
107:             $message=$this->message!==null?$this->message:Yii::t('yii','{attribute} is of the wrong length (should be {length} characters).');
108:             $this->addError($object,$attribute,$message,array('{length}'=>$this->is));
109:         }
110:     }
111: 
112:     /**
113:      * Returns the JavaScript needed for performing client-side validation.
114:      * @param CModel $object the data object being validated
115:      * @param string $attribute the name of the attribute to be validated.
116:      * @return string the client-side validation script.
117:      * @see CActiveForm::enableClientValidation
118:      * @since 1.1.7
119:      */
120:     public function clientValidateAttribute($object,$attribute)
121:     {
122:         $label=$object->getAttributeLabel($attribute);
123: 
124:         if(($message=$this->message)===null)
125:             $message=Yii::t('yii','{attribute} is of the wrong length (should be {length} characters).');
126:         $message=strtr($message, array(
127:             '{attribute}'=>$label,
128:             '{length}'=>$this->is,
129:         ));
130: 
131:         if(($tooShort=$this->tooShort)===null)
132:             $tooShort=Yii::t('yii','{attribute} is too short (minimum is {min} characters).');
133:         $tooShort=strtr($tooShort, array(
134:             '{attribute}'=>$label,
135:             '{min}'=>$this->min,
136:         ));
137: 
138:         if(($tooLong=$this->tooLong)===null)
139:             $tooLong=Yii::t('yii','{attribute} is too long (maximum is {max} characters).');
140:         $tooLong=strtr($tooLong, array(
141:             '{attribute}'=>$label,
142:             '{max}'=>$this->max,
143:         ));
144: 
145:         $js='';
146:         if($this->min!==null)
147:         {
148:             $js.="
149: if(value.length<{$this->min}) {
150:     messages.push(".CJSON::encode($tooShort).");
151: }
152: ";
153:         }
154:         if($this->max!==null)
155:         {
156:             $js.="
157: if(value.length>{$this->max}) {
158:     messages.push(".CJSON::encode($tooLong).");
159: }
160: ";
161:         }
162:         if($this->is!==null)
163:         {
164:             $js.="
165: if(value.length!={$this->is}) {
166:     messages.push(".CJSON::encode($message).");
167: }
168: ";
169:         }
170: 
171:         if($this->allowEmpty)
172:         {
173:             $js="
174: if(jQuery.trim(value)!='') {
175:     $js
176: }
177: ";
178:         }
179: 
180:         return $js;
181:     }
182: }
183: 
184: 
API documentation generated by ApiGen 2.8.0