PageRenderTime 40ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/common/lib/Yii/validators/CStringValidator.php

https://bitbucket.org/haichau59/manga
PHP | 177 lines | 92 code | 12 blank | 73 comment | 21 complexity | be6193f9fce476aef63f4067c52fda67 MD5 | raw file
  1. <?php
  2. /**
  3. * CStringValidator 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. * CStringValidator validates that the attribute value is of certain length.
  12. *
  13. * Note, this validator should only be used with string-typed attributes.
  14. *
  15. * In addition to the {@link message} property for setting a custom error message,
  16. * CStringValidator has a couple custom error messages you can set that correspond to different
  17. * validation scenarios. For defining a custom message when the string is too short,
  18. * you may use the {@link tooShort} property. Similarly with {@link tooLong}. The messages may contain
  19. * placeholders that will be replaced with the actual content. In addition to the "{attribute}"
  20. * placeholder, recognized by all validators (see {@link CValidator}), CStringValidator allows for the following
  21. * placeholders to be specified:
  22. * <ul>
  23. * <li>{min}: when using {@link tooShort}, replaced with minimum length, {@link min}, if set.</li>
  24. * <li>{max}: when using {@link tooLong}, replaced with the maximum length, {@link max}, if set.</li>
  25. * <li>{length}: when using {@link message}, replaced with the exact required length, {@link is}, if set.</li>
  26. * </ul>
  27. *
  28. * @author Qiang Xue <qiang.xue@gmail.com>
  29. * @version $Id$
  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. * 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(function_exists('mb_strlen') && $this->encoding!==false)
  82. $length=mb_strlen($value, $this->encoding ? $this->encoding : Yii::app()->charset);
  83. else
  84. $length=strlen($value);
  85. if($this->min!==null && $length<$this->min)
  86. {
  87. $message=$this->tooShort!==null?$this->tooShort:Yii::t('yii','{attribute} is too short (minimum is {min} characters).');
  88. $this->addError($object,$attribute,$message,array('{min}'=>$this->min));
  89. }
  90. if($this->max!==null && $length>$this->max)
  91. {
  92. $message=$this->tooLong!==null?$this->tooLong:Yii::t('yii','{attribute} is too long (maximum is {max} characters).');
  93. $this->addError($object,$attribute,$message,array('{max}'=>$this->max));
  94. }
  95. if($this->is!==null && $length!==$this->is)
  96. {
  97. $message=$this->message!==null?$this->message:Yii::t('yii','{attribute} is of the wrong length (should be {length} characters).');
  98. $this->addError($object,$attribute,$message,array('{length}'=>$this->is));
  99. }
  100. }
  101. /**
  102. * Returns the JavaScript needed for performing client-side validation.
  103. * @param CModel $object the data object being validated
  104. * @param string $attribute the name of the attribute to be validated.
  105. * @return string the client-side validation script.
  106. * @see CActiveForm::enableClientValidation
  107. * @since 1.1.7
  108. */
  109. public function clientValidateAttribute($object,$attribute)
  110. {
  111. $label=$object->getAttributeLabel($attribute);
  112. if(($message=$this->message)===null)
  113. $message=Yii::t('yii','{attribute} is of the wrong length (should be {length} characters).');
  114. $message=strtr($message, array(
  115. '{attribute}'=>$label,
  116. '{length}'=>$this->is,
  117. ));
  118. if(($tooShort=$this->tooShort)===null)
  119. $tooShort=Yii::t('yii','{attribute} is too short (minimum is {min} characters).');
  120. $tooShort=strtr($tooShort, array(
  121. '{attribute}'=>$label,
  122. '{min}'=>$this->min,
  123. ));
  124. if(($tooLong=$this->tooLong)===null)
  125. $tooLong=Yii::t('yii','{attribute} is too long (maximum is {max} characters).');
  126. $tooLong=strtr($tooLong, array(
  127. '{attribute}'=>$label,
  128. '{max}'=>$this->max,
  129. ));
  130. $js='';
  131. if($this->min!==null)
  132. {
  133. $js.="
  134. if(value.length<{$this->min}) {
  135. messages.push(".CJSON::encode($tooShort).");
  136. }
  137. ";
  138. }
  139. if($this->max!==null)
  140. {
  141. $js.="
  142. if(value.length>{$this->max}) {
  143. messages.push(".CJSON::encode($tooLong).");
  144. }
  145. ";
  146. }
  147. if($this->is!==null)
  148. {
  149. $js.="
  150. if(value.length!={$this->is}) {
  151. messages.push(".CJSON::encode($message).");
  152. }
  153. ";
  154. }
  155. if($this->allowEmpty)
  156. {
  157. $js="
  158. if($.trim(value)!='') {
  159. $js
  160. }
  161. ";
  162. }
  163. return $js;
  164. }
  165. }