PageRenderTime 26ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/mysites/framework/validators/CNumberValidator.php

https://gitlab.com/muthuvel.ns/imp-file
PHP | 175 lines | 96 code | 10 blank | 69 comment | 19 complexity | ee4e7c50c7c3ca86956b4be72b9366d1 MD5 | raw file
  1. <?php
  2. /**
  3. * CNumberValidator class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright Copyright &copy; 2008-2011 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($this->integerOnly)
  79. {
  80. if(!preg_match($this->integerPattern,"$value"))
  81. {
  82. $message=$this->message!==null?$this->message:Yii::t('yii','{attribute} must be an integer.');
  83. $this->addError($object,$attribute,$message);
  84. }
  85. }
  86. else
  87. {
  88. if(!preg_match($this->numberPattern,"$value"))
  89. {
  90. $message=$this->message!==null?$this->message:Yii::t('yii','{attribute} must be a number.');
  91. $this->addError($object,$attribute,$message);
  92. }
  93. }
  94. if($this->min!==null && $value<$this->min)
  95. {
  96. $message=$this->tooSmall!==null?$this->tooSmall:Yii::t('yii','{attribute} is too small (minimum is {min}).');
  97. $this->addError($object,$attribute,$message,array('{min}'=>$this->min));
  98. }
  99. if($this->max!==null && $value>$this->max)
  100. {
  101. $message=$this->tooBig!==null?$this->tooBig:Yii::t('yii','{attribute} is too big (maximum is {max}).');
  102. $this->addError($object,$attribute,$message,array('{max}'=>$this->max));
  103. }
  104. }
  105. /**
  106. * Returns the JavaScript needed for performing client-side validation.
  107. * @param CModel $object the data object being validated
  108. * @param string $attribute the name of the attribute to be validated.
  109. * @return string the client-side validation script.
  110. * @see CActiveForm::enableClientValidation
  111. * @since 1.1.7
  112. */
  113. public function clientValidateAttribute($object,$attribute)
  114. {
  115. $label=$object->getAttributeLabel($attribute);
  116. if(($message=$this->message)===null)
  117. $message=$this->integerOnly ? Yii::t('yii','{attribute} must be an integer.') : Yii::t('yii','{attribute} must be a number.');
  118. $message=strtr($message, array(
  119. '{attribute}'=>$label,
  120. ));
  121. if(($tooBig=$this->tooBig)===null)
  122. $tooBig=Yii::t('yii','{attribute} is too big (maximum is {max}).');
  123. $tooBig=strtr($tooBig, array(
  124. '{attribute}'=>$label,
  125. '{max}'=>$this->max,
  126. ));
  127. if(($tooSmall=$this->tooSmall)===null)
  128. $tooSmall=Yii::t('yii','{attribute} is too small (minimum is {min}).');
  129. $tooSmall=strtr($tooSmall, array(
  130. '{attribute}'=>$label,
  131. '{min}'=>$this->min,
  132. ));
  133. $pattern=$this->integerOnly ? $this->integerPattern : $this->numberPattern;
  134. $js="
  135. if(!value.match($pattern)) {
  136. messages.push(".CJSON::encode($message).");
  137. }
  138. ";
  139. if($this->min!==null)
  140. {
  141. $js.="
  142. if(value<{$this->min}) {
  143. messages.push(".CJSON::encode($tooSmall).");
  144. }
  145. ";
  146. }
  147. if($this->max!==null)
  148. {
  149. $js.="
  150. if(value>{$this->max}) {
  151. messages.push(".CJSON::encode($tooBig).");
  152. }
  153. ";
  154. }
  155. if($this->allowEmpty)
  156. {
  157. $js="
  158. if(jQuery.trim(value)!='') {
  159. $js
  160. }
  161. ";
  162. }
  163. return $js;
  164. }
  165. }