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