CNumberValidator.php 5.4 KB

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